DECLARE LIBRARY 'function is already used by QB64 so "User32" is not required
k = GetKey(.15)
'The delay is here so we can slow down how quickly we check for keypresses.
'Set it to 0 and see what happens when you hit a key...
'We read the keyboard state so quickly, we get multiple returns for each press.
IF v2
<> 0 THEN GetKey
= v2: v2
= 0:
EXIT FUNCTION 'return a secondary key for LEFT/RIGHT shift, ctrl CASE 8, 9, 13, 27:
IF GetAsyncKeyState
(i
) THEN v
= i:
EXIT FOR 'backspace, tab, enter, esc CASE 16 TO 18 'shift, ctrl, alt, which we deal with below with the left/right returns CASE 32 TO 40:
IF GetAsyncKeyState
(i
) THEN v
= i:
EXIT FOR 'space, pageup, pagedown, end, home, '37 to 40 left arrow, up arrow, right arrow, down arrow
'It's not a key which QB64 has included on the virtual map listing at https://www.qb64.org/wiki/Keyboard_scancodes
'BEEP
IF GetAsyncKeyState
(i
) THEN PRINT i;
" key pressed, which is not recognized. Please report this to Steve!" 'END
GetKey = v
LastReturn##
= TIMER + Delay##
v2 = 0
Working on various things over the last few days, as I've tried to relax and do some coding between the hustle and bustle of Christmas, I've sorted out that _KEYHIT and _KEYDOWN have a ton of flaws.
Shift-Tab returns the value for a CTRL-I key press.
The Menu key doesn't return a down value at all, and tries to report itself as an up bracket "]" keypress.
Various other mappings are off and unreliable, but I don't remember exactly what they are now, off the top of my head.
So, in an attempt to make use of a few of those "lost" keys, I sat down and wrote the above little routine for us -- which only works in Windows (as GetAsyncKeyState is a Windows library call). As far as I can tell, this properly tells us if we hit a key, or not.
The only issues I've found with it are:
1) No distinction between uppercase or lowercase keypresses. You'd need to write a toggle into your code for
IF _KEYDOWN(100303) or _KEYDOWN(100304) THEN, and then use it to toggle between uppercase input and lowercase.
2) It returns repeat values so dang fast, I had to code in a delay system to stop from getting multiple responses from a single key press.
I don't know if this would be useful for anyone else, as nobody else seems to ever complain about missing/incorrect key returns (maybe most people simply don't key for such events as a menu key hit, or shift-tab actions), but I thought I'd share it anyway. ;)