Recent

Author Topic: Windows: OnKeyDown and the numeric keypad  (Read 1935 times)

CuriousKit

  • Jr. Member
  • **
  • Posts: 78
Windows: OnKeyDown and the numeric keypad
« on: October 01, 2018, 07:18:47 am »
Hi everyone,

So I have a question that is probably a bit basic, but I'm having a slight issue with the OnKeyDown event.  I want to be able to detect the user pressing buttons on the numeric keypad.  The problem is, if Num Lock is turned off, it will register the buttons as if other keys were pressed (e.g. 1 on the keypad is the same as "End", and the virtual key code will be VK_END rather than VK_NUMPAD1).  Is there a way to be certain as to what key is pressed regardless of the state of Num Lock.  I know I could easily hook into the WM_KEYDOWN message and evaluate the keyboard scan code, but I rather not do that because it will break portability and scan codes are platform-specific.

ADDENDUM: I'm sensing there may be no way around this except to either evaluate the keyboard scan code, or force Num Lock to be turned on (which might be a little risky if the user normally likes it turned off).
« Last Edit: October 01, 2018, 07:51:06 am by CuriousKit »

jamie

  • Hero Member
  • *****
  • Posts: 6130
Re: Windows: OnKeyDown and the numeric keypad
« Reply #1 on: October 02, 2018, 12:58:38 am »
instead of forcing it on, why not simply test for it to be on and notify the user that they need it on?

 I guess you could also try the "GetKeyBoardState" for each key press, that copys all 255 keys into a
buffer.

I wanted to state that also there are a separate list of defines for the numpad numbers..

 if you capture the keys before allowing them to a control you can test for the
 VK_Numpad0..9.. and the other keys on the board. doing this way allows you to filter out
unwanted keys..
--
 Edit:

    It looks like you are correct, there isn't a real true way to determine the difference because if
you the numlock is off it simply duplicates a non-numpad key board.
 
   I think this came from the old days...

 Oh well.
« Last Edit: October 02, 2018, 01:30:40 am by jamie »
The only true wisdom is knowing you know nothing

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Windows: OnKeyDown and the numeric keypad
« Reply #2 on: October 02, 2018, 01:51:21 am »
If I remember correctly (and I may not, mind you) you can only do what you want by writing a keyboard interceptor so as to receive a notification, through a callback routine, before Windows process key presses to KeyDown/Up messages.

You will need to dive a little in MS's docs, though, cause I don't remember exactly how it was done.

ETA
Another possible solution: Maybe some of the gaming libs have something useful for you(?)
« Last Edit: October 02, 2018, 01:53:47 am by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

 

TinyPortal © 2005-2018