Weird keypress bug in GFX?

New to FreeBASIC? Post your questions here.
Post Reply
datwill310
Posts: 355
Joined: May 29, 2015 20:37

Weird keypress bug in GFX?

Post by datwill310 »

In the game I'm making, I've noticed a strange behaviour with my keyboard where if I hold the Function key and F11 (on my keyboard it turns up volume), it acts as a B key press in my game (I use FBGFX, multikey, and the scancodes).

Is this a bug with the library or my game that I should look out for? Or is it not a bug at all and the scancode combination of Fn+F11 = B for some reason?
Last edited by datwill310 on Jun 23, 2017 14:19, edited 1 time in total.
sancho2
Posts: 547
Joined: May 17, 2015 6:41

Re: Weird bug?

Post by sancho2 »

I can't reproduce the error, though I don't know what you mean by 'function key' and f11.
f11 is a function key. And I tried it with ctrl, alt, windows key, shift, capslock, numlock.

Code: Select all

#Include "fbgfx.bi"
Using fb
ScreenRes 800, 600
Dim l As Long = 0
Do 
	If MultiKey(SC_B) Then Cls: Print "hello"
	Print l 
	l += 1
Loop While Not InKey = Chr(27)
Another keyboard I have has a 'FN' key between the right alt, and ctrl keys. This is used to access secondary functions of certain keys.
It to does not register as a 'b' on F11 or any other function key.
I suspect it is a quirk of the driver for your keyboards extra functionality.
datwill310
Posts: 355
Joined: May 29, 2015 20:37

Re: Weird bug?

Post by datwill310 »

Thanks for your post.
I was able to trigger the "hello!" event using both B and FN+F11.
This is an image of the keyboard I am using (I don't think it's the exact model but it's close enough):
Image
In between the Alt Gr and Menu keys is the FN key I've found on pretty much all keyboards I've used. It may have been in different places on the keyboard, or in another colour altogether (usually blue). I have been surprised to not find the FN key on so many keyboards while researching though!
On this keyboard too, you can use the combination FN+F11 to turn the volume up (this is shown on the keyboard F11 key in the image above - though granted not very clearly :D) - which the driver uses to turn the volume up.
Yes, I'm thinking too that it may just be the way the driver/keyboard works. But just posting to clarify :).
TeeEmCee
Posts: 375
Joined: Jul 22, 2006 0:54
Location: Auckland

Re: Weird bug?

Post by TeeEmCee »

I think it's almost certainly a fbgfx bug, where it misinterprets some unusual keypress events.
Are you using Windows?
There is surely a utility somewhere (I could provide one if needed) which prints out information about win32 keypress events as they occur. If you posted the output of that we could look at it and try to figure out why it's happening.
datwill310
Posts: 355
Joined: May 29, 2015 20:37

Re: Weird bug?

Post by datwill310 »

TeeEmCee wrote:Are you using Windows?
There is surely a utility somewhere (I could provide one if needed) which prints out information about win32 keypress events as they occur. If you posted the output of that we could look at it and try to figure out why it's happening.
Yes, I am using Windows.
I will look for a piece of software like this and I will post any results.
datwill310
Posts: 355
Joined: May 29, 2015 20:37

Re: Weird bug?

Post by datwill310 »

TeeEmCee wrote:There is surely a utility somewhere (I could provide one if needed) which prints out information about win32 keypress events as they occur. If you posted the output of that we could look at it and try to figure out why it's happening.
I have used the software at delphiforfun.org and have found the following:
The first paragraph describes me pressing the B key, the second paragraph describes me pressing the key combination FN and F11, and the third paragraph describes me pressing the F11 key.
I have included KeyDown event exits.
OnKeyDown, Key code=66, Control keys=, Key name b
OnKeyPress b
OnKeyup, Key code=66, Control keys=, Key name b

OnKeyDown, Key code=175, Control keys=, Key name ..........Unassigned
OnKeyup, Key code=175, Control keys=, Key name ..........Unassigned

OnKeyDown, Key code=122, Control keys=, Key name F11
OnKeyup, Key code=122, Control keys=, Key name F11
TeeEmCee
Posts: 375
Joined: Jul 22, 2006 0:54
Location: Auckland

Re: Weird bug?

Post by TeeEmCee »

OK, thanks. Key 175 is VK_VOLUME_UP.

I just had a look at the code, and it seems that multikey does not return the results from listening to win32 events. It depends on which fbgfx driver is in use: there are three: gdi, opengl and ddraw. ddraw uses DirectInput to poll the keyboard state, while the others use the win32 function GetKeyboardState.
It doesn't appear that the GetKeyboardState code could possibly create a bad 'B' key press, but the DirectInput codepath looks really dubious. In fact, I notice that a keycode of 176 would cause 'B' to appear to be pressed, and maybe there is an off-by-one I'm not aware of.

Could you check which driver is in use?

Code: Select all

' (Whatever 'screen' command)
dim driver as string
screeninfo  , , , , , , driver
?driver
datwill310
Posts: 355
Joined: May 29, 2015 20:37

Re: Weird bug?

Post by datwill310 »

TeeEmCee wrote:Could you check which driver is in use?

Code: Select all

' (Whatever 'screen' command)
dim driver as string
screeninfo  , , , , , , driver
?driver
I've not actually put it in the game code, but I've initialised the same screen mode with the same settings and am using DirectX.
TeeEmCee
Posts: 375
Joined: Jul 22, 2006 0:54
Location: Auckland

Re: Weird keypress bug in GFX?

Post by TeeEmCee »

Looking closer, I see this is really complicated. The ddraw driver calls IDirectInputDevice_SetDataFormat to set the format of the array of data (keystate in the code below) that it wants: it's given an array of 256 key specifications. I don't understand the specifications, the documentation is surprisingly unhelpful, and fbgfx's source code has been obfuscated by not using the original constant and macro names from the documentation.

But then at gfx_driver_ddraw.c (lin 460) certain keys are aliased together:

Code: Select all

                /* Simplicistic way to deal with extended scancodes */
                for (i = 0; i < 128; i++)
                    __fb_gfx->key[i] = ((keystate[i] | keystate[i + 128]) & 0x80) ? TRUE : FALSE;
I think the comment points to the bug.

If you look at this list of DirectInput scan codes you see that F11 is 47. And it's close to FB's scancode for B, which is 48.
Also I notice that VK_VOLUME_UP, 175, is 128+47, a modified version of the F11 key?

I don't have more time to look into this right now. Maybe in a couple weeks.
datwill310
Posts: 355
Joined: May 29, 2015 20:37

Re: Weird keypress bug in GFX?

Post by datwill310 »

TeeEmCee wrote:Looking closer, I see this is really complicated.
This IS complicated o_O. I want to thank you for doing this.
adele
Posts: 47
Joined: Jun 13, 2015 19:33

Re: Weird keypress bug in GFX?

Post by adele »

Hi datwill310,

obviously you try to assign certain keystrokes to actions in your game. As a general driver cannot "know" all KB layouts (and the corresponding return codes) I suggest to let the user define which keyboard hits should be assigned to these actions. I had seen this kind of "setup" in many old MS-DOS programs, even on CBM C64 machines, and your program just would have to "remember" the scan codes, not even the name of the key actually chosen.

Let us assume you want to assign a key to the function "move right", no matter if the user hits one of the keys "r", ">" or <right> or <right on numpad> or s.th. totally unexpected, the chosen key can be associated with the function.

Just an idea...

adi
datwill310
Posts: 355
Joined: May 29, 2015 20:37

Re: Weird keypress bug in GFX?

Post by datwill310 »

adele wrote:Hi datwill310,

obviously you try to assign certain keystrokes to actions in your game. As a general driver cannot "know" all KB layouts (and the corresponding return codes) I suggest to let the user define which keyboard hits should be assigned to these actions. I had seen this kind of "setup" in many old MS-DOS programs, even on CBM C64 machines, and your program just would have to "remember" the scan codes, not even the name of the key actually chosen.

Let us assume you want to assign a key to the function "move right", no matter if the user hits one of the keys "r", ">" or <right> or <right on numpad> or s.th. totally unexpected, the chosen key can be associated with the function.

Just an idea...

adi
Thanks for the idea, I do allow the user to chose between pretty much all the keyboard scan codes for every action ;) - if this is what you mean. And you can even use a gamepad.
I was talking about the B key because that is the default key for one of those actions and I'm using the default controls.
EDIT: or do you mean assigning multiple keys to one action?
jmgbsas
Posts: 35
Joined: Dec 26, 2020 16:03

Re: Weird keypress bug in GFX?

Post by jmgbsas »

Hi,
I am starting using scancode and Multikey, and I notice that when the program increase
in size or big events multikey fail and i dicover that in fbgfx.bi the enum is using hexadecimal values as &h44 , but if I replace for 68 run ok, the enum should be better in decimal not in hexadecimal
for example
SC_F9 = 67
SC_F10 = 68
SC_NUMLOCK
...
here are all scan codes http://philipstorr.id.au/pcbook/book3/scancode.htm
only is necesary to conver the hexa to decimal and set in the enum reclacing the &hxx
greetings from buenos aires..
paul doe
Moderator
Posts: 1733
Joined: Jul 25, 2017 17:22
Location: Argentina

Re: Weird keypress bug in GFX?

Post by paul doe »

jmgbsas wrote:...
I am starting using scancode and Multikey, and I notice that when the program increase
in size or big events multikey fail and i dicover that in fbgfx.bi the enum is using hexadecimal values as &h44 , but if I replace for 68 run ok, the enum should be better in decimal not in hexadecimal
...
This is unlikely the reason. Perhaps you're 'eating' events when you're polling? Can you post a snippet highlighting the behavior?
jmgbsas
Posts: 35
Joined: Dec 26, 2020 16:03

Re: Weird keypress bug in GFX?

Post by jmgbsas »

I add code but was a sql error here...Ibelieve that you are right
if multikey (SC_A)
if multikey (SC_A) and multikey(SC_CONTROL) then
....or
if multikey (SC_A) and multikey(SC_CONTROL) then

if multikey (SC_A) then

has is erratic behavior because start with the same capture I change to
if multikey(SC_CONTROL) and multikey (SC_A) then
..
if multikey (SC_A) then
By the way, I think ALT_GRAPH is the same as left ALT has the same value for that reason it doesn't run
with & h64 ... I used the multikey.bas program posted on this forum.
I modified the enum I feel more comfortable with decimals and added my comments for the Spanish keyboard. It works the same.
enum ' tested and update with spanish keyoard and decimals
SC_ESCAPE = 1
SC_1 = 2
SC_2 = 3
SC_3 = 4
SC_4 = 5
SC_5 = 6
SC_6 = 7
SC_7 = 8
SC_8 = 9
SC_9 = 10
SC_0 = 11
SC_MINUS = 12 ' normal and keypad
SC_PLUS = 13 ' normal keyboard spanish
SC_BACKSPACE = 14
SC_TAB = 15
SC_Q = 16
SC_W = 17
SC_E = 18
SC_R = 19
SC_T = 20
SC_Y = 21
SC_U = 22
SC_I = 23
SC_O = 24
SC_P = 25
SC_LEFTBRACKET = 26 ' TILDE spanish keyboard
SC_RIGHTBRACKET = 27 ' ¡ spanish keyboard
SC_ENTER = 28
SC_CONTROL = 29 '1D..LEFT, RIGHT = 224, 29
SC_A = 30 ' 1E
SC_S = 31
SC_D = 32 ' h20
SC_F = 33
SC_G = 34 ' h22
SC_H = 35
SC_J = 36
SC_K = 37
SC_L = 38
SC_SEMICOLON = 39 ' &h27 ; ..+ shift= :
SC_QUOTE = 40 ' ('..+shift= " )
SC_TILDE = 41 ' (`) &h29... ñ in spanish
SC_LSHIFT = 42 ' &h2A
SC_BACKSLASH = 43 'spanish keyboard = (º)
SC_Z = 44 ' 2C
SC_X = 45 ' 2D
SC_C = 46
SC_V = 47
SC_B = 48 ' h30
SC_N = 49
SC_M = 50
SC_COMMA = 51 '
SC_PERIOD = 52 ' normal keypad
SC_SLASH = 53 ' keypad /, normal spanish keyoard = ç ¿?
SC_RSHIFT = 54
SC_MULTIPLY = 55 ' keypad *
SC_ALT = 56
SC_SPACE = 57 ' h39
SC_CAPSLOCK = 58 ' h40
SC_F1 = 59
SC_F2 = 60
SC_F3 = 61
SC_F4 = 62
SC_F5 = 63
SC_F6 = 64
SC_F7 = 65
SC_F8 = 66
SC_F9 = 67
SC_F10 = 68
SC_NUMLOCK = 69
SC_SCROLLLOCK = 70
SC_HOME = 71
SC_UP = 72
SC_PAGEUP = 73 ' &h49
'' &h4A unused (?) ' keypad - (12), should be 74
SC_LEFT = 75 ' E0 4B, 224 75
SC_CLEAR = 76 ' KEYPAD 5, MULTIKEY NO DETECT? TEST
SC_RIGHT = 77
SC_KEYPADPLUS = 78 ' keypad +
SC_END = 79 'normal and keypad
SC_DOWN = 80 'normal and keypad
SC_PAGEDOWN = 81 'normal and keypad
SC_INSERT = 82 'normal and keypad
SC_DELETE = 83 'normal or (+numlock=keypad period, -numlock=keypd delete) )
'' &h54 'shift + F1 should be http://philipstorr.id.au/pcbook/book3/table1.htm
'' &h55 'shift + F2 should be
'' &h56 'shift + F3 should be
SC_F11 = 87 ' shift + F4 should be
SC_F12 = 88 'shift + F5 should be
'' &h59 ' shift + F6 should be
'' &h5A 'shift + F7 should be
SC_LWIN = 91 '&h5B 'shift + F8 should be
SC_RWIN = 92
SC_MENU = 93
'' &h5E ' Ctrl F1 should be
'' &h5F ' Ctrl F2 should be
'' &h60 ' Ctrl F3 should be
'' &h61 ' Ctrl F4 should be
'' &h62 ' Ctrl F5 should be
'' &h63 ' Ctrl F6 should be
SC_ALTGR = 56 '' = SC_ALT
end enum

Thanks
Post Reply