Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Keypad Entry
#1
I am working on a project that requires keypad entry.

The problem is that QB64 does not trap them!?

Here is my code:

Code: (Select All)
Rem Keypad-5 = 76
Rem Shift-Keypad-5 = 53
Rem Ctrl-Keypad-5 = 143
Do
  X$ = InKey$
  If Len(X$) Then
      If X$ = Chr$(27) Then End
      If Len(X$) = 2 Then
        X = Asc(Right$(X$, 1))
        Select Case X
            Case 76
              Print "keypad-5"
            Case 53
              Print "shift-keypad-5"
            Case 143
              Print "ctrl-keypad-5"
        End Select
      End If
  End If
Loop
End
Reply
#2
Use _KEYHIT or _KEYDOWN instead.

https://qb64phoenix.com/qb64wiki/index.php/KEYHIT

https://qb64phoenix.com/qb64wiki/index.php/KEYDOWN
Reply
#3
(01-07-2023, 06:22 AM)mnrvovrfc Wrote: Use _KEYHIT or _KEYDOWN instead.

https://qb64phoenix.com/qb64wiki/index.php/KEYHIT

https://qb64phoenix.com/qb64wiki/index.php/KEYDOWN

I tried this code with _keyhit and still does not return keypad-5:

Code: (Select All)
Rem Keypad-5 = 76
Rem Shift-Keypad-5 = 53
Rem Ctrl-Keypad-5 = 143
Do
  X = _KeyHit
  If X Then
      Print "Keypress="; X
      If X = 27 Then End
      If X >= 256 And X <= 65535 Then
        X = (X And &HFF00) / 256
        Print "Ascii2="; X
        Select Case X
            Case 76
              Print "keypad-5"
            Case 53
              Print "shift-keypad-5"
            Case 143
              Print "ctrl-keypad-5"
        End Select
      End If
  End If
Loop
End
Reply
#4
SMcNeill probably knows your pain best as he has posted special key handling code a number of times.

If he doesn't pipe in, try some searches with him posting.
b = b + ...
Reply
#5
Aye.  It's an issue which faces not just QB64, but anything built upon glut as its core.  We're working on it, but until then, you might want to make use of my keyboard library or use windows native functions for your program.
Reply
#6
Quote:I am working on a project that requires keypad entry.

Are you looking for ASCII input (with NumLock on), or navigation key input (with NumLock off)?

Either version of your test program should return proper ASCII values for the digit keys if NumLock is on.


I modified your second tester to print only the raw data that it sees, and also to print that data to a text file "k2out.txt":

Code: (Select All)
Rem Keypad-5 = 76
Rem Shift-Keypad-5 = 53
Rem Ctrl-Keypad-5 = 143

Open "o", 1, "k2out.txt"

Do
    X = _KeyHit
    If X Then
        Print "Keypress="; X; "("; (X And &HFF00) / 256; ")"
        Print #1, "Keypress="; X; "("; (X And &HFF00) / 256; ")"
        If X = 27 Then End
    End If
Loop

Close

End

and....

Each key sends a "make" code when pressed (or held down), and a "break" code when released, except for numpad-5....

With NumLock on, each key (including numpad-5) sends the appropriate ASCII value when the key is pressed or held down.  It sends a sign-flipped ASCII value when the key is released.

With NumLock off, each key sends a "make" code when the key is pressed (these will also repeat if held down), and also a "break" code when released, EXCEPT for numpad-5, which has no alternate non-numeric function.  Numpad-5 sends nothing when pressed or held, and only a single, non-repeating "break" code when the key is released.


Below are tables of the keycodes my modified tester sees.
The first table is the result of pressing & releasing the numpad keys 4, 5, & 6 with NumLock on.
The second table is the result of pressing & releasing the numpad keys 4, 5, & 6 with NumLock off.


With NumLock on:
4:  Keypress= 52 ( 0 )
    Keypress=-52 (-1 )

5:  Keypress= 53 ( 0 )
    Keypress=-53 (-1 )

6:  Keypress= 54 ( 0 )
    Keypress=-54 (-1 )

ESC: Keypress= 27 ( 0 )


==============================
==============================
==============================


With NumLock off:
4:  Keypress= 19200 ( 75 )
    Keypress=-19200 (-75 )

5:                                       (NOTE: no "make" code is sent when NumLock is off)
    Keypress=-12 (-1 )

6:  Keypress= 19712 ( 77 )
    Keypress=-19712 (-77 )

ESC: Keypress= 27 ( 0 )
Reply
#7
I am using numlock off and pressing keypad-5 and get the same results..

Should I assume a return value of -12 indicates keypad-5?

Erik.
Reply
#8
This might be the main reason why some programs bid the user to make sure "NUMLOCK" is off. Then that "5" number pad key isn't even used.

A "portable" way is just to allow the user to choose his/her keypresses, or to kindly ask him/her to make sure "NUMLOCK" is on or off, and then work on things from there. Also warn there will be no guarantees about functionality if the user doesn't follow instructions. Shy

Remember also that your program could be used by somebody on a budget laptop, which is too small, or was designed in such a way to leave no room for a numeric keypad. I have two HP computers which are this way. I have another computer, ASUS which has a trick to use the touchpad as numeric keypad. Huh
Reply
#9
(01-09-2023, 03:48 AM)eoredson Wrote: I am using numlock off and pressing keypad-5 and get the same results..

Should I assume a return value of -12 indicates keypad-5?

Erik.

It might be worth a try.
My quick test does not show any other key returning -12.

I'm sorry, but I know very little about the internals of QB64PE and don't know where those key values are being generated.
Reply
#10
I happened to be awake the other night when this was first posted and played around with some code to come up with a solution. I was surprised that I could not get this to work properly either in the 45 minutes I tinkered with it. I see from the replies that it wasn't just me.
Reply




Users browsing this thread: 5 Guest(s)