Author Topic: _KEYDOWN with alphanumeric keys  (Read 1935 times)

0 Members and 1 Guest are viewing this topic.

Offline johannhowitzer

  • Forum Regular
  • Posts: 118
_KEYDOWN with alphanumeric keys
« on: July 19, 2019, 10:38:12 pm »
When I use _KEYDOWN with a value such as 100304 for left shift, it works fine, but everything I try for alphanumeric keys doesn't work - such as _KEYDOWN(90) or _KEYDOWN(100090) for the z key.  Is this impossible, or is the wiki / help documentation incorrect?

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
Re: _KEYDOWN with alphanumeric keys
« Reply #1 on: July 19, 2019, 10:50:19 pm »
Looks OK to me:
Code: QB64: [Select]
  1. WHILE _KEYDOWN(27) = 0
  2.     FOR i = 65 TO 90
  3.         IF _KEYDOWN(i) THEN PRINT CHR$(i),
  4.     NEXT
  5.     FOR i = 97 TO 122
  6.         IF _KEYDOWN(i) THEN PRINT CHR$(i),
  7.     NEXT
  8.     _LIMIT 100
  9.  
  10.  

Offline johannhowitzer

  • Forum Regular
  • Posts: 118
Re: _KEYDOWN with alphanumeric keys
« Reply #2 on: July 19, 2019, 11:12:13 pm »
Code: QB64: [Select]
  1. screen _newimage(640, 480, 12)
  2.  
  3. const true = -1
  4. const false = 0
  5.  
  6. const up& = 18432
  7. const right& = 19712
  8. const down& = 20480
  9. const left& = 19200
  10.  
  11. const lshift& = 100304
  12. const lctrl& = 100306
  13.  
  14.    _limit 60: _display
  15.    cls
  16.    if _keydown(up&) = true then print "UP"
  17.    if _keydown(right&) = true then print "RIGHT"
  18.    if _keydown(down&) = true then print "DOWN"
  19.    if _keydown(left&) = true then print "LEFT"
  20.  
  21.    if _keydown(lshift&) = true then print "LSHIFT"
  22.    if _keydown(lctrl&) = true then print "LCTRL"
  23.    if _keydown(90) = true then print "Z"
  24.    if _keydown(88) = true then print "X"
  25.  

Your code is working, but mine isn't.  It prints everything but the Z and X.

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • Steve’s QB64 Archive Forum
Re: _KEYDOWN with alphanumeric keys
« Reply #3 on: July 19, 2019, 11:23:38 pm »
Also try _KEYDOWN(122).  There’s a different return code for lowercase and uppercase versions of letters.
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
Re: _KEYDOWN with alphanumeric keys
« Reply #4 on: July 19, 2019, 11:27:58 pm »
You could use INKEY$ with a PEEK routine...

Code: QB64: [Select]
  1.     _LIMIT 30
  2.  
  3.     DEF SEG = 0
  4.     ii = PEEK(&H417) MOD 16
  5.     SELECT CASE ii
  6.         CASE 0
  7.             IF shift THEN PRINT "shift key up": shift = 0
  8.             IF ctrl THEN PRINT "ctrl key up": ctrl = 0
  9.         CASE 1, 2
  10.             shift = -1
  11.             PRINT "shift key down"
  12.         CASE 4
  13.             ctrl = -1
  14.             PRINT "ctrl key down"
  15.     END SELECT
  16.     DEF SEG
  17.  
  18.     key$ = INKEY$
  19.     SELECT CASE key$
  20.         CASE CHR$(27)
  21.             END
  22.         CASE CHR$(32) TO CHR$(126)
  23.             PRINT key$
  24.     END SELECT

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

FellippeHeitor

  • Guest
Re: _KEYDOWN with alphanumeric keys
« Reply #5 on: July 19, 2019, 11:38:15 pm »
Let peek and poke rest. If _KEYHIT/_KEYDOWN can’t get it, find the bug in the code, as per McNeil’s suggestion.

Offline johannhowitzer

  • Forum Regular
  • Posts: 118
Re: _KEYDOWN with alphanumeric keys
« Reply #6 on: July 19, 2019, 11:52:42 pm »
Huh, the uppercase version works.  I wonder why bplus' code worked and mine didn't.  Still, that solves my problem.

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • Steve’s QB64 Archive Forum
Re: _KEYDOWN with alphanumeric keys
« Reply #7 on: July 19, 2019, 11:58:39 pm »
Huh, the uppercase version works.  I wonder why bplus' code worked and mine didn't.  Still, that solves my problem.

Bplus trapped for both uppercase and lowercase; you need to do the same.

IF _KEYDOWN(90) = true OR _KEYDOWN(122) = true THEN....

Depending on CapsLock state, you’ll generate different key codes.

“Z” is not the same keypress as “z”.
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
Re: _KEYDOWN with alphanumeric keys
« Reply #8 on: July 20, 2019, 12:32:41 am »
Let peek and poke rest. If _KEYHIT/_KEYDOWN can’t get it, find the bug in the code, as per McNeil’s suggestion.

What? Them's fightin' words varmint!

OK, fine, use Windows API, instead...

Code: QB64: [Select]
  1. CONST VK_SHIFT = &H10
  2. CONST VK_CONTROL = &H11
  3.  
  4.     FUNCTION GetAsyncKeyState% (BYVAL nul%)
  5.  
  6.     shift% = GetAsyncKeyState%(VK_SHIFT): IF shift% THEN shiftdown% = -1: PRINT "shift key down"
  7.     ctrl% = GetAsyncKeyState(VK_CONTROL): IF ctrl% THEN ctrldown% = -1: PRINT "ctrl key down"
  8.  
  9.     IF shiftdown% AND shift% = 0 THEN PRINT "shift key up": shiftdown% = 0
  10.     IF ctrldown% AND ctrl% = 0 THEN PRINT "ctrl key up": ctrldown% = 0
  11.  
  12.     key$ = INKEY$
  13.     SELECT CASE key$
  14.         CASE CHR$(27)
  15.             END
  16.         CASE CHR$(32) TO CHR$(126)
  17.             PRINT key$
  18.     END SELECT
  19.  

 - Sam
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 + ...
Re: _KEYDOWN with alphanumeric keys
« Reply #9 on: July 20, 2019, 12:39:00 am »
Hi johannhowitzer,

When I hold down the shift key and hold down the z key, your code will print "Z" for me, using vers 1.3 on Windows 10 Laptop.

Remember _KEYDOWN detects keys that are being held down, no recorded buffer.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
Re: _KEYDOWN with alphanumeric keys
« Reply #10 on: July 20, 2019, 12:41:29 am »
What? Them's fightin' words varmint!

OK, fine, use Windows API, instead...

Code: QB64: [Select]
  1. CONST VK_SHIFT = &H10
  2. CONST VK_CONTROL = &H11
  3.  
  4.     FUNCTION GetAsyncKeyState% (BYVAL nul%)
  5.  
  6.     shift% = GetAsyncKeyState%(VK_SHIFT): IF shift% THEN shiftdown% = -1: PRINT "shift key down"
  7.     ctrl% = GetAsyncKeyState(VK_CONTROL): IF ctrl% THEN ctrldown% = -1: PRINT "ctrl key down"
  8.  
  9.     IF shiftdown% AND shift% = 0 THEN PRINT "shift key up": shiftdown% = 0
  10.     IF ctrldown% AND ctrl% = 0 THEN PRINT "ctrl key up": ctrldown% = 0
  11.  
  12.     key$ = INKEY$
  13.     SELECT CASE key$
  14.         CASE CHR$(27)
  15.             END
  16.         CASE CHR$(32) TO CHR$(126)
  17.             PRINT key$
  18.     END SELECT
  19.  

 - Sam

LOL! Can't kill a fly so call in the SWAT team! :D

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
Re: _KEYDOWN with alphanumeric keys
« Reply #11 on: July 20, 2019, 01:42:44 am »
SWAT the hell are ya talking about now? Oh, ya wants ta know hows ta do it with INP? Well, why didn't ya say so, varmint?

Code: QB64: [Select]
  1.     scancode% = INP(&H60)
  2.     SELECT CASE scancode%
  3.         CASE 29
  4.             IF ctrl% = 0 THEN PRINT "ctrl key down": ctrl% = -1
  5.         CASE 29 + 128
  6.             IF ctrl% THEN PRINT "ctrl key up": ctrl% = 0
  7.         CASE 42
  8.             IF lshift% = 0 THEN PRINT "left shift key down": lshift% = -1
  9.         CASE 42 + 128
  10.             IF lshift% THEN PRINT "left shift key up": lshift% = 0
  11.         CASE 54
  12.             IF rshift% = 0 THEN PRINT "right shift key down": rshift% = -1
  13.         CASE 54 + 128
  14.             IF rshift% THEN PRINT "right shift key up": rshift% = 0
  15.     END SELECT
  16.  
  17.     key$ = INKEY$
  18.     SELECT CASE key$
  19.         CASE CHR$(27)
  20.             END
  21.         CASE CHR$(32) TO CHR$(126)
  22.             PRINT key$
  23.     END SELECT
  24.  

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

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
Re: _KEYDOWN with alphanumeric keys
« Reply #12 on: July 20, 2019, 12:02:44 pm »
Hi Pete
fine INP use!

Using on my Toshiba with external USB Keyboard it doesn't detect right Alt key  and detects as Ctrl Key  the left Alt key...
Programming isn't difficult, only it's  consuming time and coffee

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
Re: _KEYDOWN with alphanumeric keys
« Reply #13 on: July 20, 2019, 12:14:16 pm »
Well you know what they say, Toshiba, Japanese for "I'm sorry!"

Those are the correct scan codes for those keys. For fun, I just plugged in my USB Insignia keyboard to my HP laptop, and the keys work correctly. I have no idea why you are getting different results. Maybe someone who knows more about the hardware can answer that question.

Could you also run the PEEK and Windows API code, and see if you get the same results?

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

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
Re: _KEYDOWN with alphanumeric keys
« Reply #14 on: July 20, 2019, 12:42:40 pm »
Hi Pete

Yes I have enlarged my experience with your codes...
I get the same results both using INP code both using Windows API code!
And more I tried both using USB  Sigma Keyboard both using internal notebook keyboard!

That's all

PS using internal Keyboard I have no limit of _KEYDOWN detected holding more than one key
Programming isn't difficult, only it's  consuming time and coffee