Step 1: Copy the following and paste it into your QB64 folder as "Test1.h"
int32 ConsoleKeyhit (int32 toggle);
int32 ConsoleKeyhit (int32 toggle)
{
HANDLE hStdin = GetStdHandle (STD_INPUT_HANDLE);
INPUT_RECORD irInputRecord;
DWORD dwEventsRead;
INT32 cChar;
while(ReadConsoleInputA (hStdin, &irInputRecord, 1, &dwEventsRead)) /* Read key press */
if (irInputRecord.EventType == KEY_EVENT)
{
cChar = irInputRecord.Event.KeyEvent.wVirtualScanCode;
if (toggle) {
if (!irInputRecord.Event.KeyEvent.bKeyDown) cChar = -cChar;
}else{
if (!irInputRecord.Event.KeyEvent.bKeyDown) cChar = cChar +128;
}
return cChar;
}
return EOF;
}
Step 2: Copy and paste the following code in QB64.
_DEST _CONSOLE 'Required to get the scan codes, and I haven't sorted out a way to bypass this line yet.
TOGGLE = -1 'Change TRUE/FALSE value to see the different return values for codes.
'See the INP Scan Codes at https://www.qb64.org/wiki/Keyboard_scancodes
a = ConsoleKeyhit(0) 'To retrieve the INP Scancodes as QB64 lists them
a = ConsoleKeyhit(1) 'To retrieve positive/negative codes for down/up codes
IF a
= 57 THEN TOGGLE
= NOT TOGGLE
'Spacebar to Swap toggle values
Step 3: Compile, Run, and Test.
As QB64 currently exists, we can only get INPUT from the CONSOLE, which necessitates typing something and then hitting ENTER.
This code allows us to get single character presses, one at a time, from the console.
Now, I was playing around with trying to have this return the _KEYHIT codes for us, but it never seemed to work quite perfect to me. Keystrokes like SHIFT-A would require 2 key presses to register, or else hitting A would return 2 different key strokes. It just doesn't work as one would expect -- at least not without a little more work than I'm willing to put into it, at the moment, with my limited freetime.
Instead, what I've came up with is the above, which returns INP scan codes for us, without any issues arising from SHIFT being up/down and trying to read a key release that just hasn't happened yet.
INP ScanCodes can be viewed here:
https://www.qb64.org/wiki/Keyboard_scancodes
NOTE: Currently this has a toggle parameter which an user can make use of.
Personally, I'm not so keen on the Code + 128 method to detect key releases; I find it
much simpler to simply have a positive and negative value for them. Positive value means the key was pressed. Negative value means the key was released.
Other folks might like the way QB64 deals with the INP codes, so I've implemented a toggle so you can choose withever method you prefer. Keypresses are the same code, no matter what toggle method you choose -- just the release codes are different.
NOTE 2: Since this uses Windows API routines, it's
*NOT* going to work for Mac/Linux folks. I don't have a PC with either of those OSes on them, and no great experience coding for either system, so I don't have a clue how we'd get the same functionality on those systems. I'm afraid this is a Windows-Only method, unless somebody more familiar than me wants to take up the effort to write us their equivalent code.
Test this out for a while, and offer any feedback or issues which you folks might have with it. It should be easy enough to add to QB64 itself -- if folks would like to see it added to the language -- but it deserves a good set of testing first to make certain it doesn't do anything unexpected for us.