Scancodes - mkilgore/QB64pe GitHub Wiki
This page is maintained for historic purposes. The functionality of the code below can now be achieved through the _KEYDOWN function.
Scancodes are the return values of keyboard input that can be read using INP to find key presses and releases, unlike the ASCII codes returned using INKEY$.
- scanCode% = INP(&H60)
- Used to find [Ctrl], [Alt], [Shift], [Caps], [Scroll], [Num] and [F]unction key controls.
- The keyboard input is stored at decimal register 96 or &H60 hexadecimal.
- Returns values from 0 to 127 for keypresses. 129 to 255 for key releases. Codes 91 to 93 are available in QB64 only.
- Every key (except Prt Scr/Sys Rq) has an identifying code that will not change with Caps Lock or Shift.
- Release codes are the original key press code + 128. Example: [Esc] key press = 1 and release = 129.
- Extended (added) keys on modern keyboards may alternate codes with Left Shift codes 42 and 170 because of Num Lock mode.
- Keyboards with Alt Gr key: _KEYHIT may return both Alt (100307) and Ctrl (100306) codes when key is pressed or released.
- Linux with foreign keyboards: SHELL _HIDE "setxkbmap us" can be used to setup a keyboard to read US scan codes.
- To clear the keyboard buffer, use the INKEY$ function before or after the INP read to avoid buffer overflows and beeps.
' '''Extended Keyboard Press Scancodes''' ' '''' Esc F1 F2 F3 F4 F5 F6 F7 F8 F9 F10 F11 F12 SysReq ScrL Pause''' ' 1 59 60 61 62 63 64 65 66 67 68 87 88 0 70 29 ' '''`~ 1! 2@ 3# 4$ 5% 6^ 7& 8* 9( 0) -_ =+ BkSpc Insert Home PgUp NumL / * -''' ' 41 2 3 4 5 6 7 8 9 10 11 12 13 14 82 71 73 69 53 55 74 ' '''Tab Q W E R T Y U I O P [{ ]} \| Delete End PgDn 7/Home 8/▲ 9/PU + ''' ' 15 16 17 18 19 20 21 22 23 24 25 26 27 43 83 79 81 71 72 73 78 ' '''CapL A S D F G H J K L ;: '" Enter 4/◄- 5 6/-► E''' ' 58 30 31 32 33 34 35 36 37 38 39 40 28 75 76 77 '''n''' ' '''Shift Z X C V B N M ,< .> /? Shift ▲ 1/End 2/▼ 3/PD t''' ' 42 44 45 46 47 48 49 50 51 52 53 54 72 79 80 81 '''e''' ' '''Ctrl Win Alt Spacebar Alt Win Menu Ctrl ◄- ▼ -► 0/Insert ./Del r''' ' 29 <span style="color:purple;">91</span> 56 57 56 <span style="color:purple;">92 93</span> 29 75 80 77 82 83 28 ' ' <span style="color:purple;">QB64 codes only!</span> '''Release codes = Press code + 128. Pause/Break may lock code returns.''' |
- In QBasic, extended keys would have the Left Shift release code 224 alternate with the actual release code. Extended keys are the Arrow keypad, Home keypad, Right Alt, and Right Ctrl keys on 101 + extended keyboards with the arrow pad, home pad and number pad.
Example 1: Displays the scan codes for most any keyboard press or release by the user.
'' '' DO scancode% = INP(&H60) LOCATE 15, 30 IF scancode% < 128 THEN COLOR 10: PRINT "Pressed Code ="; scancode%; SPACE$(1) IF scancode% > 127 THEN COLOR 12: PRINT "Release Code ="; scancode%; SPACE$(1) a$ = INKEY$ ' prevent keyboard beeps LOOP UNTIL scancode% = 1 ' [Esc] key exit |
Explanation: Green is a press code and red is the release code. Num Lock mode may display the Left Shift key press(42) and release(170) codes, plus code 224, but the designated codes are returned also. QB64 does not return those extra codes.
Example 2: Unlike QBasic, INP(&H60) in QB64 reads from a queue so you never miss a key press.
row = 2 'display the key down states to show you how it works! FOR x% = 1 TO 88 'read each key scancode from KeyDown array L = x% MOD 10: column = (7 * L) + 5 IF L = 0 THEN row = row + 2 LOCATE row, column IF KeyPress(x%) THEN PRINT USING tmp$; x%; STRING$(2, 219) ELSE PRINT USING tmp$; x%; "UP" NEXT LOOP UNTIL i = 1 DEF SEG END '' '' |
Example 3: A simple MULTIKEY demo using the SC Array to hold multiple key presses for diagonal arrow key moves.
GET (graphics statement) (x, y)-(x + 15, y + 15), BG ' GET original BG at start box position LINE (x, y)-(x + 15, y + 15), 9, BF ' the plain blue box to move GET (graphics statement) (x, y)-(x + 15, y + 15), Box ' GET to Box Array DO 'main loop t! = TIMER + .05 DO ' 1 Tick (1/18th second) keypress scancode read loop a$ = INKEY$ ' So the keyboard buffer won't get full code% = INP(&H60) ' Get keyboard scan code from port 96 IF...THEN code% < 128 THEN SC(code%) = 1 ELSE SC(code% - 128) = 0 ' true/false values to array LOOP UNTIL TIMER > t!' loop until one tick has passed PX = x: PY = y ' previous coordinates IF SC(75) = 1 THEN x = x - 5: IF x < 0 THEN x = 0 IF SC(77) = 1 THEN x = x + 5: IF x > 304 THEN x = 304 IF SC(72) = 1 THEN y = y - 5: IF y < 0 THEN y = 0 IF SC(80) = 1 THEN y = y + 5: IF y > 184 THEN y = 184 IF x <> PX OR y <> PY THEN ' look for a changed coordinate value WAIT 936, 8: PUT (graphics statement) (PX, PY), BG, PSET ' replace previous BG first GET (graphics statement) (x, y)-(x + 15, y + 15), BG ' GET BG at new position before box is set PUT (graphics statement) (x, y), Box, PSET ' PUT box image at new position END IF LOOP UNTIL SC(1) = 1 ' main loop until [Esc] key (scan code 1) is pressed |
Navigation:
Go to Keyword Reference - Alphabetical
Go to Keyword Reference - By usage
Go to Main WIKI Page