Author Topic: INKEY$ in console mode  (Read 3273 times)

0 Members and 1 Guest are viewing this topic.

Offline blametroi

  • Newbie
  • Posts: 26
    • View Profile
INKEY$ in console mode
« on: June 12, 2019, 08:39:11 am »
I'm reviving some old text mode programs where I used INKEY$ to deal with single character input such as with a Y-or-N response to a question. When run to a console window, it doesn't work. I went over the wiki page and see a mention of using _DEST _CONSOLE but that doesn't seem to change anything.

Here's a minimal sample program using some of the INKEY$ code from the wiki. If the first two lines are uncommented, the program does not respond to any keypress.

It looks as if the program window, as opposed to console window, is intercepting the keypresses and not sending them through to the console. You can see this if you just use "$CONSOLE" instead of "$CONSOLE:ONLY". If you give the program window focus, the keypresses go through.

Any guidance? Thanks.

Code: QB64: [Select]
  1. '$CONSOLE:ONLY
  2. '_DEST _CONSOLE
  3.  
  4.     PRINT "Do you want to continue? (Y/N): "; 'semicolon saves position for user entry
  5.     DO: K$ = UCASE$(INKEY$) 'change any user key press to uppercase
  6.     LOOP UNTIL K$ = "Y" OR K$ = "N"
  7.     PRINT K$ 'print valid user entry
  8.     IF K$ = "N" THEN END
  9.  

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: INKEY$ in console mode
« Reply #1 on: June 12, 2019, 09:08:56 am »
hmm... looks like INKEY$ not available to CONSOLE, INPUT works but not what is wanted:
Code: QB64: [Select]
  1.     PRINT "Do you want to continue? (Y/N): "; 'semicolon saves position for user entry
  2.     INPUT ""; K$
  3.     K$ = UCASE$(K$)
  4.     'DO
  5.     '    _DELAY .01  'put console window in front
  6.     '    K$ = UCASE$(INKEY$) 'change any user key press to uppercase
  7.     'LOOP UNTIL K$ = "Y" OR K$ = "N"
  8.     PRINT K$ 'print valid user entry
  9.     IF K$ = "N" THEN END
  10.  
  11.  
  12.  
« Last Edit: June 12, 2019, 09:37:17 am by bplus »

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: INKEY$ in console mode
« Reply #2 on: June 12, 2019, 10:08:56 am »
_KEYHIT and _KEYDOWN don't seem to work in $CONSOLE:ONLY either.
« Last Edit: June 12, 2019, 10:20:07 am by bplus »

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: INKEY$ in console mode
« Reply #3 on: June 12, 2019, 11:32:15 am »
Windows API call would be my way to attack it.

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

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: INKEY$ in console mode
« Reply #4 on: June 12, 2019, 11:59:03 am »
Man! all that just because you don't want an additional enter button press. ;-))

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: INKEY$ in console mode
« Reply #5 on: June 12, 2019, 12:06:32 pm »
Hey could you job outsource with a key press routine in a hidden SHELL? Return back with the Y or N or whatever...

Offline Raven_Singularity

  • Forum Regular
  • Posts: 158
    • View Profile
Re: INKEY$ in console mode
« Reply #6 on: June 12, 2019, 12:09:28 pm »
Just write a low-level keyboard driver.  :clownface:


Edit:
To be clear, this was a joke, not an actual suggestion.  :-)
« Last Edit: June 12, 2019, 08:05:43 pm by Raven_Singularity »

Offline blametroi

  • Newbie
  • Posts: 26
    • View Profile
Re: INKEY$ in console mode
« Reply #7 on: June 12, 2019, 12:28:20 pm »
Meh. I know how to do the various alternatives, I was hoping to avoid breaking out of the core language. The doc on the wiki says it should work. This was a pretty common practice in BASIC programming back in the day.

I'll cast about and see if I can find the wndproc for the program window. I think it should pass the wm_keypress type messages on but seems not to do so.