Author Topic: INKEY to VK conversion function  (Read 555 times)

MrBcx

  • Administrator
  • Hero Member
  • *****
  • Posts: 2538
    • View Profile
INKEY to VK conversion function
« on: July 01, 2024, 09:09:48 PM »
This works for -most- keys but there are a few misfires.  For example,
PageUp and PageDown don't get ID'd correctly on my keyboard. Test it out for
yourself to see if it will be a good fit for your projects.

Code: [Select]
'=================================================================
' Convert_To_VK Function converts most BCX INKEY/KEYPRESS values
' to the corresponding WinAPI VK_ (virtual key) constant values.
' By MrBcx  July 2, 2024                         Public Domain
'=================================================================
' This is not perfect but it might be good enough for your needs.
'=================================================================

'======================  BEGIN DEMONSTRATION  ====================

DIM ky, vk AS INTEGER

DO
    ky = INKEY
    IF ky = 0 THEN ITERATE
    IF ky < 0 THEN

        vk = Convert_To_VK(ky)   ' <<<----  The MAGIC happens here

        SELECT CASE vk
        CASE VK_PAUSE : PRINT "You pressed the VK_PAUSE"
        CASE VK_ESCAPE : PRINT "You pressed the VK_ESCAPE"
        CASE VK_SPACE : PRINT "You pressed the VK_SPACE"
        CASE VK_END : PRINT "You pressed the VK_END"
        CASE VK_HOME : PRINT "You pressed the VK_HOME"
        CASE VK_LEFT : PRINT "You pressed the VK_LEFT"
        CASE VK_UP : PRINT "You pressed the VK_UP"
        CASE VK_RIGHT : PRINT "You pressed the VK_RIGHT"
        CASE VK_DOWN : PRINT "You pressed the VK_DOWN"
        CASE VK_INSERT : PRINT "You pressed the VK_INSERT"
        CASE VK_DELETE : PRINT "You pressed the VK_DELETE"
        CASE VK_NUMPAD0 : PRINT "You pressed the VK_NUMPAD0"
        CASE VK_NUMPAD1 : PRINT "You pressed the VK_NUMPAD1"
        CASE VK_NUMPAD2 : PRINT "You pressed the VK_NUMPAD2"
        CASE VK_NUMPAD3 : PRINT "You pressed the PAGEDOWN"    ' There is no VK_PAGEDOWN, so I'm using this
        CASE VK_NUMPAD4 : PRINT "You pressed the VK_NUMPAD4"
        CASE VK_NUMPAD5 : PRINT "You pressed the VK_NUMPAD5"
        CASE VK_NUMPAD6 : PRINT "You pressed the VK_NUMPAD6"
        CASE VK_NUMPAD7 : PRINT "You pressed the VK_NUMPAD7"
        CASE VK_NUMPAD8 : PRINT "You pressed the VK_NUMPAD8"
        CASE VK_NUMPAD9 : PRINT "You pressed the PAGEUP"       ' There is no VK_PAGEUP, so I'm using this
        CASE VK_MULTIPLY : PRINT "You pressed the VK_MULTIPLY"
        CASE VK_ADD : PRINT "You pressed the VK_ADD"
        CASE VK_SEPARATOR : PRINT "You pressed the VK_SEPARATOR"
        CASE VK_SUBTRACT : PRINT "You pressed the VK_SUBTRACT"
        CASE VK_DECIMAL : PRINT "You pressed the VK_DECIMAL"
        CASE VK_DIVIDE : PRINT "You pressed the VK_DIVIDE"
        CASE VK_F1 : PRINT "You pressed the VK_F1"
        CASE VK_F2 : PRINT "You pressed the VK_F2"
        CASE VK_F3 : PRINT "You pressed the VK_F3"
        CASE VK_F4 : PRINT "You pressed the VK_F4"
        CASE VK_F5 : PRINT "You pressed the VK_F5"
        CASE VK_F6 : PRINT "You pressed the VK_F6"
        CASE VK_F7 : PRINT "You pressed the VK_F7"
        CASE VK_F8 : PRINT "You pressed the VK_F8"
        CASE VK_F9 : PRINT "You pressed the VK_F9"
        CASE VK_F10 : PRINT "You pressed the VK_F10"
        CASE VK_F11 : PRINT "You pressed the VK_F11"
        CASE VK_F12 : PRINT "You pressed the VK_F12"
        END SELECT
    ELSE
        IF ky = VK_TAB THEN PRINT "You pressed the VK_TAB key" : ITERATE
        IF ky = VK_RETURN  THEN PRINT "You pressed the VK_RETURN key" : ITERATE
        IF ky = VK_BACK  THEN PRINT "You pressed the VK_BACK key" : ITERATE
        IF ky = VK_ESCAPE  THEN PRINT "You pressed the VK_ESCAPE key" : ITERATE
        PRINT "You pressed the "; CHR$(ABS(ky)); " Key"
    END IF
LOOP

'====================== END OF DEMONSTRATION =====================


TYPE KeyMapping
    VK_Value AS INTEGER
    INKEY_Value AS INTEGER
    INKEY_Value_NumLock_Disabled AS INTEGER
END TYPE



FUNCTION Convert_To_VK (Inkey_Value AS INTEGER) AS INTEGER
    STATIC KeyMap[78] AS KeyMapping
    KeyMap[0].VK_Value = 8 : KeyMap[0].INKEY_Value = 8 : KeyMap[0].INKEY_Value_NumLock_Disabled = 0
    KeyMap[1].VK_Value = 9 : KeyMap[1].INKEY_Value = 9 : KeyMap[1].INKEY_Value_NumLock_Disabled = 0
    KeyMap[2].VK_Value = 13 : KeyMap[2].INKEY_Value = 13 : KeyMap[2].INKEY_Value_NumLock_Disabled = 0
    KeyMap[3].VK_Value = 19 : KeyMap[3].INKEY_Value = -69 : KeyMap[3].INKEY_Value_NumLock_Disabled = 0
    KeyMap[4].VK_Value = 27 : KeyMap[4].INKEY_Value = 27 : KeyMap[4].INKEY_Value_NumLock_Disabled = 0
    KeyMap[5].VK_Value = 32 : KeyMap[5].INKEY_Value = 32 : KeyMap[5].INKEY_Value_NumLock_Disabled = 0
    KeyMap[6].VK_Value = 35 : KeyMap[6].INKEY_Value = -79 : KeyMap[6].INKEY_Value_NumLock_Disabled = 0
    KeyMap[7].VK_Value = 36 : KeyMap[7].INKEY_Value = -71 : KeyMap[7].INKEY_Value_NumLock_Disabled = 0
    KeyMap[8].VK_Value = 37 : KeyMap[8].INKEY_Value = -75 : KeyMap[8].INKEY_Value_NumLock_Disabled = 0
    KeyMap[9].VK_Value = 38 : KeyMap[9].INKEY_Value = -72 : KeyMap[9].INKEY_Value_NumLock_Disabled = 0
    KeyMap[10].VK_Value = 39 : KeyMap[10].INKEY_Value = -77 : KeyMap[10].INKEY_Value_NumLock_Disabled = 0
    KeyMap[11].VK_Value = 40 : KeyMap[11].INKEY_Value = -80 : KeyMap[11].INKEY_Value_NumLock_Disabled = 0
    KeyMap[12].VK_Value = 45 : KeyMap[12].INKEY_Value = -82 : KeyMap[12].INKEY_Value_NumLock_Disabled = 0
    KeyMap[13].VK_Value = 46 : KeyMap[13].INKEY_Value = -83 : KeyMap[13].INKEY_Value_NumLock_Disabled = 0
    KeyMap[14].VK_Value = 48 : KeyMap[14].INKEY_Value = 48 : KeyMap[14].INKEY_Value_NumLock_Disabled = 0
    KeyMap[15].VK_Value = 49 : KeyMap[15].INKEY_Value = 49 : KeyMap[15].INKEY_Value_NumLock_Disabled = 0
    KeyMap[16].VK_Value = 50 : KeyMap[16].INKEY_Value = 50 : KeyMap[16].INKEY_Value_NumLock_Disabled = 0
    KeyMap[17].VK_Value = 51 : KeyMap[17].INKEY_Value = 51 : KeyMap[17].INKEY_Value_NumLock_Disabled = 0
    KeyMap[18].VK_Value = 52 : KeyMap[18].INKEY_Value = 52 : KeyMap[18].INKEY_Value_NumLock_Disabled = 0
    KeyMap[19].VK_Value = 53 : KeyMap[19].INKEY_Value = 53 : KeyMap[19].INKEY_Value_NumLock_Disabled = 0
    KeyMap[20].VK_Value = 54 : KeyMap[20].INKEY_Value = 54 : KeyMap[20].INKEY_Value_NumLock_Disabled = 0
    KeyMap[21].VK_Value = 55 : KeyMap[21].INKEY_Value = 55 : KeyMap[21].INKEY_Value_NumLock_Disabled = 0
    KeyMap[22].VK_Value = 56 : KeyMap[22].INKEY_Value = 56 : KeyMap[22].INKEY_Value_NumLock_Disabled = 0
    KeyMap[23].VK_Value = 57 : KeyMap[23].INKEY_Value = 57 : KeyMap[23].INKEY_Value_NumLock_Disabled = 0
    KeyMap[24].VK_Value = 65 : KeyMap[24].INKEY_Value = 65 : KeyMap[24].INKEY_Value_NumLock_Disabled = 0
    KeyMap[25].VK_Value = 66 : KeyMap[25].INKEY_Value = 66 : KeyMap[25].INKEY_Value_NumLock_Disabled = 0
    KeyMap[26].VK_Value = 67 : KeyMap[26].INKEY_Value = 67 : KeyMap[26].INKEY_Value_NumLock_Disabled = 0
    KeyMap[27].VK_Value = 68 : KeyMap[27].INKEY_Value = 68 : KeyMap[27].INKEY_Value_NumLock_Disabled = 0
    KeyMap[28].VK_Value = 69 : KeyMap[28].INKEY_Value = 69 : KeyMap[28].INKEY_Value_NumLock_Disabled = 0
    KeyMap[29].VK_Value = 70 : KeyMap[29].INKEY_Value = 70 : KeyMap[29].INKEY_Value_NumLock_Disabled = 0
    KeyMap[30].VK_Value = 71 : KeyMap[30].INKEY_Value = 71 : KeyMap[30].INKEY_Value_NumLock_Disabled = 0
    KeyMap[31].VK_Value = 72 : KeyMap[31].INKEY_Value = 72 : KeyMap[31].INKEY_Value_NumLock_Disabled = 0
    KeyMap[32].VK_Value = 73 : KeyMap[32].INKEY_Value = 73 : KeyMap[32].INKEY_Value_NumLock_Disabled = 0
    KeyMap[33].VK_Value = 74 : KeyMap[33].INKEY_Value = 74 : KeyMap[33].INKEY_Value_NumLock_Disabled = 0
    KeyMap[34].VK_Value = 75 : KeyMap[34].INKEY_Value = 75 : KeyMap[34].INKEY_Value_NumLock_Disabled = 0
    KeyMap[35].VK_Value = 76 : KeyMap[35].INKEY_Value = 76 : KeyMap[35].INKEY_Value_NumLock_Disabled = 0
    KeyMap[36].VK_Value = 77 : KeyMap[36].INKEY_Value = 77 : KeyMap[36].INKEY_Value_NumLock_Disabled = 0
    KeyMap[37].VK_Value = 78 : KeyMap[37].INKEY_Value = 78 : KeyMap[37].INKEY_Value_NumLock_Disabled = 0
    KeyMap[38].VK_Value = 79 : KeyMap[38].INKEY_Value = 79 : KeyMap[38].INKEY_Value_NumLock_Disabled = 0
    KeyMap[39].VK_Value = 80 : KeyMap[39].INKEY_Value = 80 : KeyMap[39].INKEY_Value_NumLock_Disabled = 0
    KeyMap[40].VK_Value = 81 : KeyMap[40].INKEY_Value = 81 : KeyMap[40].INKEY_Value_NumLock_Disabled = 0
    KeyMap[41].VK_Value = 82 : KeyMap[41].INKEY_Value = 82 : KeyMap[41].INKEY_Value_NumLock_Disabled = 0
    KeyMap[42].VK_Value = 83 : KeyMap[42].INKEY_Value = 83 : KeyMap[42].INKEY_Value_NumLock_Disabled = 0
    KeyMap[43].VK_Value = 84 : KeyMap[43].INKEY_Value = 84 : KeyMap[43].INKEY_Value_NumLock_Disabled = 0
    KeyMap[44].VK_Value = 85 : KeyMap[44].INKEY_Value = 85 : KeyMap[44].INKEY_Value_NumLock_Disabled = 0
    KeyMap[45].VK_Value = 86 : KeyMap[45].INKEY_Value = 86 : KeyMap[45].INKEY_Value_NumLock_Disabled = 0
    KeyMap[46].VK_Value = 87 : KeyMap[46].INKEY_Value = 87 : KeyMap[46].INKEY_Value_NumLock_Disabled = 0
    KeyMap[47].VK_Value = 88 : KeyMap[47].INKEY_Value = 88 : KeyMap[47].INKEY_Value_NumLock_Disabled = 0
    KeyMap[48].VK_Value = 89 : KeyMap[48].INKEY_Value = 89 : KeyMap[48].INKEY_Value_NumLock_Disabled = 0
    KeyMap[49].VK_Value = 90 : KeyMap[49].INKEY_Value = 90 : KeyMap[49].INKEY_Value_NumLock_Disabled = 0
    KeyMap[50].VK_Value = 96 : KeyMap[50].INKEY_Value = 48 : KeyMap[50].INKEY_Value_NumLock_Disabled = -82
    KeyMap[51].VK_Value = 97 : KeyMap[51].INKEY_Value = 49 : KeyMap[51].INKEY_Value_NumLock_Disabled = -79
    KeyMap[52].VK_Value = 98 : KeyMap[52].INKEY_Value = 50 : KeyMap[52].INKEY_Value_NumLock_Disabled = -80
    KeyMap[53].VK_Value = 99 : KeyMap[53].INKEY_Value = 51 : KeyMap[53].INKEY_Value_NumLock_Disabled = -81
    KeyMap[54].VK_Value = 100 : KeyMap[54].INKEY_Value = 52 : KeyMap[54].INKEY_Value_NumLock_Disabled = -75
    KeyMap[55].VK_Value = 101 : KeyMap[55].INKEY_Value = 53 : KeyMap[55].INKEY_Value_NumLock_Disabled = -76
    KeyMap[56].VK_Value = 102 : KeyMap[56].INKEY_Value = 54 : KeyMap[56].INKEY_Value_NumLock_Disabled = -77
    KeyMap[57].VK_Value = 103 : KeyMap[57].INKEY_Value = 55 : KeyMap[57].INKEY_Value_NumLock_Disabled = -71
    KeyMap[58].VK_Value = 104 : KeyMap[58].INKEY_Value = 56 : KeyMap[58].INKEY_Value_NumLock_Disabled = -72
    KeyMap[59].VK_Value = 105 : KeyMap[59].INKEY_Value = 57 : KeyMap[59].INKEY_Value_NumLock_Disabled = -73
    KeyMap[60].VK_Value = 106 : KeyMap[60].INKEY_Value = 42 : KeyMap[60].INKEY_Value_NumLock_Disabled = 42
    KeyMap[61].VK_Value = 107 : KeyMap[61].INKEY_Value = 43 : KeyMap[61].INKEY_Value_NumLock_Disabled = 43
    KeyMap[62].VK_Value = 108 : KeyMap[62].INKEY_Value = 44 : KeyMap[62].INKEY_Value_NumLock_Disabled = 44
    KeyMap[63].VK_Value = 109 : KeyMap[63].INKEY_Value = 45 : KeyMap[63].INKEY_Value_NumLock_Disabled = 45
    KeyMap[64].VK_Value = 110 : KeyMap[64].INKEY_Value = 46 : KeyMap[64].INKEY_Value_NumLock_Disabled = -83
    KeyMap[65].VK_Value = 111 : KeyMap[65].INKEY_Value = 47 : KeyMap[65].INKEY_Value_NumLock_Disabled = 47
    KeyMap[66].VK_Value = 112 : KeyMap[66].INKEY_Value = -59 : KeyMap[66].INKEY_Value_NumLock_Disabled = 0
    KeyMap[67].VK_Value = 113 : KeyMap[67].INKEY_Value = -60 : KeyMap[67].INKEY_Value_NumLock_Disabled = 0
    KeyMap[68].VK_Value = 114 : KeyMap[68].INKEY_Value = -61 : KeyMap[68].INKEY_Value_NumLock_Disabled = 0
    KeyMap[69].VK_Value = 115 : KeyMap[69].INKEY_Value = -62 : KeyMap[69].INKEY_Value_NumLock_Disabled = 0
    KeyMap[70].VK_Value = 116 : KeyMap[70].INKEY_Value = -63 : KeyMap[70].INKEY_Value_NumLock_Disabled = 0
    KeyMap[71].VK_Value = 117 : KeyMap[71].INKEY_Value = -64 : KeyMap[71].INKEY_Value_NumLock_Disabled = 0
    KeyMap[72].VK_Value = 118 : KeyMap[72].INKEY_Value = -65 : KeyMap[72].INKEY_Value_NumLock_Disabled = 0
    KeyMap[73].VK_Value = 119 : KeyMap[73].INKEY_Value = -66 : KeyMap[73].INKEY_Value_NumLock_Disabled = 0
    KeyMap[74].VK_Value = 120 : KeyMap[74].INKEY_Value = -67 : KeyMap[74].INKEY_Value_NumLock_Disabled = 0
    KeyMap[75].VK_Value = 121 : KeyMap[75].INKEY_Value = -68 : KeyMap[75].INKEY_Value_NumLock_Disabled = 0
    KeyMap[76].VK_Value = 122 : KeyMap[76].INKEY_Value = -87 : KeyMap[76].INKEY_Value_NumLock_Disabled = 0
    KeyMap[77].VK_Value = 123 : KeyMap[77].INKEY_Value = -88 : KeyMap[77].INKEY_Value_NumLock_Disabled = 0
    FOR INT i = 0 TO 77
        IF KeyMap[i].INKEY_Value = Inkey_Value OR KeyMap[i].INKEY_Value_NumLock_Disabled = Inkey_Value THEN
            FUNCTION = KeyMap[i].VK_Value
            EXIT FUNCTION
        END IF
    NEXT
    FUNCTION = 0
END FUNCTION

« Last Edit: July 02, 2024, 12:57:33 PM by MrBcx »

Pietro54

  • Newbie
  • *
  • Posts: 25
    • View Profile
Re: INKEY to VK conversion function
« Reply #1 on: July 02, 2024, 07:35:07 AM »
Hi

PgUp doesn't work but if I change line 143 to
   KeyMap[59].INKEY_Value_NumLock_Disabled = -73
it seems to work fine.

MrBcx

  • Administrator
  • Hero Member
  • *****
  • Posts: 2538
    • View Profile
Re: INKEY to VK conversion function
« Reply #2 on: July 02, 2024, 09:56:54 AM »
Hi

PgUp doesn't work but if I change line 143 to
   KeyMap[59].INKEY_Value_NumLock_Disabled = -73
it seems to work fine.

Thank you Pietro ... I've updated my code above.

Now when PageDown and PageUp are pressed, it explicitly says so.


This works when NUMLOCK is disabled.

CASE VK_NUMPAD3 : PRINT "You pressed the PAGEDOWN"

CASE VK_NUMPAD9 : PRINT "You pressed the PAGEUP"


When NUMLOCK is enabled, the number pad's  #3 and #9 keys
return the ASCII code for the numbers 3 and 9, as they should.


VK_PRIOR and VK_NEXT are aliases for the PgUp and PgDn but I can't get them to work.

https://learn.microsoft.com/en-us/windows/win32/inputdev/virtual-key-codes


« Last Edit: July 02, 2024, 10:06:53 AM by MrBcx »

Quin

  • Sr. Member
  • ****
  • Posts: 299
    • View Profile
    • Quin's site
Re: INKEY to VK conversion function
« Reply #3 on: July 05, 2024, 02:29:36 AM »
If the goal is to hook the numpad, is it possible to make all the keys work? They commonly get hooked by, for example, the ASUS V2.0 trackpad for my reading keys globally. It is probably a fairly standard map (7 is previous line, 8 is current, 9 is next etc.), the next row is word, below that is character. We could maybe even merge the (finished) function into BCX's stdlib, if it gets good enough. I don't know how okay Kevin is with having patched functions like this in the core and I still don't have enough knowledge about these APIs to say on my own what we should maybe do with certainty, but I'm personally okay with it (it matches the style of bcx.ini and things), in the core it would solve this issue).  8)

MrBcx

  • Administrator
  • Hero Member
  • *****
  • Posts: 2538
    • View Profile
Re: INKEY to VK conversion function
« Reply #4 on: July 05, 2024, 02:22:21 PM »
If the goal is to hook the numpad, is it possible to make all the keys work? They commonly get hooked by, for example, the ASUS V2.0 trackpad for my reading keys globally. It is probably a fairly standard map (7 is previous line, 8 is current, 9 is next etc.), the next row is word, below that is character. We could maybe even merge the (finished) function into BCX's stdlib, if it gets good enough. I don't know how okay Kevin is with having patched functions like this in the core and I still don't have enough knowledge about these APIs to say on my own what we should maybe do with certainty, but I'm personally okay with it (it matches the style of bcx.ini and things), in the core it would solve this issue).  8)

Not everything needs to be built into BCX and certainly not the code presented in this thread.

If someone needs INKEY / VK_ mapping, the code in this thread should serve as a good springboard.