Author Topic: Not so much a bug but inconsistent  (Read 3880 times)

0 Members and 1 Guest are viewing this topic.

This topic contains a post which is marked as Best Answer. Press here if you would like to see it.

Offline doppler

  • Forum Regular
  • Posts: 241
    • View Profile
Not so much a bug but inconsistent
« on: July 25, 2019, 06:47:32 pm »
This is not much of a bug like the subject line says.  It's part of the extended QB64 functions so we can just say.  "That's how it's gonna work."

If function "END" is use the program would ask to "Press any key to continue".

What's inconsistent:

$CONSOLE:ONLY
_ECHO "Hello"
END

The program ends with message "Press enter to continue".  And it really wants the enter key only.

At least the END does not multi-thread max the CPU out anymore.  That was an old ending bug.
And YES I want to use the END function, otherwise the window would just go whizzing on by too fast to
read and scrollback later.

So which one do we got here, is it a BUG or Feature ?

FellippeHeitor

  • Guest
Re: Not so much a bug but inconsistent
« Reply #1 on: July 25, 2019, 07:32:48 pm »
Not a bug or feature, but a workaround. We can’t read inkey$ in the console, so the way to trigger a pause is to wait for input. ENTER marks the end of user input (notice you can still type while the message is displayed).

Offline doppler

  • Forum Regular
  • Posts: 241
    • View Profile
Re: Not so much a bug but inconsistent
« Reply #2 on: July 25, 2019, 08:18:59 pm »

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Not so much a bug but inconsistent
« Reply #3 on: July 25, 2019, 09:03:18 pm »
Not a bug or feature, but a workaround. We can’t read inkey$ in the console, so the way to trigger a pause is to wait for input. ENTER marks the end of user input (notice you can still type while the message is displayed).

Here's a way to read  inkey$ with console:

Step 1:  Copy the following and save it inside your QV64 folder as "Test.h":

Code: C++: [Select]
  1. extern int32 ConsoleKeyhit (VOID);
  2.  
  3. int32 ConsoleKeyhit (VOID)
  4. {
  5.   HANDLE hStdin = GetStdHandle (STD_INPUT_HANDLE);
  6.   INPUT_RECORD irInputRecord;
  7.   DWORD dwEventsRead;
  8.   CHAR cChar;
  9.  
  10.   while(ReadConsoleInputA (hStdin, &irInputRecord, 1, &dwEventsRead)) /* Read key press */
  11.     if (irInputRecord.EventType == KEY_EVENT
  12.         &&irInputRecord.Event.KeyEvent.wVirtualKeyCode != VK_SHIFT
  13.         &&irInputRecord.Event.KeyEvent.wVirtualKeyCode != VK_MENU
  14.         &&irInputRecord.Event.KeyEvent.wVirtualKeyCode != VK_CONTROL)
  15.     {
  16.       cChar = irInputRecord.Event.KeyEvent.uChar.AsciiChar;
  17.         ReadConsoleInputA (hStdin, &irInputRecord , 1, &dwEventsRead); /* Read key release */
  18.         return cChar;
  19.     }
  20.   return EOF;
  21. }

Step 2: Make use of DECLARE LIBRARY to use the command, as such:

Code: QB64: [Select]
  1.     FUNCTION ConsoleKeyhit
  2.  
  3.  
  4. _ECHO "Hello"
  5.     a = ConsoleKeyhit
  6.     IF a <> 0 THEN PRINT a
  7. LOOP UNTIL a = 27

Compile and run, and you should be able to read in the keycodes for keys one at a time as you press them, without having to wait and trap for the ENTER key for the end of input stream.
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

FellippeHeitor

  • Guest
Re: Not so much a bug but inconsistent
« Reply #4 on: July 25, 2019, 09:41:59 pm »
Here's a way to read  inkey$ with console:

Thanks for sharing.

Marked as best answer by doppler on July 26, 2019, 05:29:25 am

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Not so much a bug but inconsistent
« Reply #5 on: July 25, 2019, 10:24:12 pm »
Here's a way to read  inkey$ with console:

It's actually more of a way to get _KEYHIT style codes back from the console. 

Tweaking it a bit more, I find that this version of Test.h gives us better results to mimic what we'd expect to see from _KEYHIT better:

Code: C++: [Select]
  1. extern int32 ConsoleKeyhit (VOID);
  2.  
  3. int32 ConsoleKeyhit (VOID)
  4. {
  5.   HANDLE hStdin = GetStdHandle (STD_INPUT_HANDLE);
  6.   INPUT_RECORD irInputRecord;
  7.   DWORD dwEventsRead;
  8.   INT32 cChar;
  9.  
  10.   while(ReadConsoleInputA (hStdin, &irInputRecord, 1, &dwEventsRead)) /* Read key press */
  11.     if (irInputRecord.EventType == KEY_EVENT
  12.         &&irInputRecord.Event.KeyEvent.wVirtualKeyCode != VK_SHIFT
  13.         &&irInputRecord.Event.KeyEvent.wVirtualKeyCode != VK_MENU
  14.         &&irInputRecord.Event.KeyEvent.wVirtualKeyCode != VK_CONTROL)
  15.     {
  16.       cChar = irInputRecord.Event.KeyEvent.uChar.AsciiChar;
  17.       if (cChar == 0) cChar = 256 * irInputRecord.Event.KeyEvent.wVirtualScanCode; //translate to an extended ASCII value, to match _KEYHIT codes.
  18.       //NOTE: SHIFT and other modifier keys are still giving incorrect codes.  They need a little tweaking to work nicely.
  19.       if (cChar>90) ReadConsoleInputA (hStdin, &irInputRecord , 1, &dwEventsRead); /* Read key release */
  20.       if (cChar<65) ReadConsoleInputA (hStdin, &irInputRecord , 1, &dwEventsRead); /* Read key release */
  21.           return cChar;
  22.     }
  23.   return EOF;
  24. }
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline Ashish

  • Forum Resident
  • Posts: 630
  • Never Give Up!
    • View Profile
Re: Not so much a bug but inconsistent
« Reply #6 on: July 26, 2019, 08:09:48 am »
Thanks Steve. This is helpful. Why not add this as new command to QB64.
if (Me.success) {Me.improve()} else {Me.tryAgain()}


My Projects - https://github.com/AshishKingdom?tab=repositories
OpenGL tutorials - https://ashishkingdom.github.io/OpenGL-Tutorials

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Not so much a bug but inconsistent
« Reply #7 on: July 26, 2019, 10:00:27 am »
Thanks Steve. This is helpful. Why not add this as new command to QB64.

It doesn't work quite as expected by trying to return _KEYHIT codes (it's an issue with knowing when to read/clear the key up press either requiring a double keypress {such as if we try ti SHIFT-ENTER}, or else returning a false double press result.)  Instead, I've tweaked it so that it works as expected, (at least as I'd expect it to), with INP Scan Codes.

To find the newest (and hopefully glitch free) version, pop over to the new topic here: https://www.qb64.org/forum/index.php?topic=1535.0
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: Not so much a bug but inconsistent
« Reply #8 on: July 27, 2019, 06:53:50 am »
Funny, Mike at The QB Forum had something similar to the recording key press and key release with INP(96)

https://www.tapatalk.com/groups/qbasic/returning-to-qbasic-after-so-many-years-t39527.html#p212907

Pete
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/