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.
'=================================================================
' 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