Keyboard scancodes

From QB64 Wiki

Jump to: navigation, search

The scancodes as they are displayed in the QB help system. If your keyboard uses different key scancodes go to:


(Keyboard scancodes)


  • Scan codes are read by using: code = INP(&H60) in a Basic procedure. The return is in decimal form unless HEX$ is used.
  • Using Shift or Caps Lock has no affect on the code returned. Codes only reflect the specific key pressed or last released.
  • Release codes returned are 128 more than the press code. Useful to stop a press code.
  • The Number Lock status may affect "extended" keys(such as the right shift, Alt, Home pad and the arrow pad) release codes.
  • Use INKEY$ to clear the keyboard buffer before or after the code is read to prevent overflow "beeping".


Keyboard Press Codes

Scancodes often used for games: Arrow Keys ALT: 38 (56) ESC: 01 (1) UP: 48 (72) CTRL: 1D (29) ENTER: 1C (28) DOWN: 50 (80) R Shift: 36 (54) SPACE: 39 (57) LEFT: 4B (75) L Shift: 2A (42) RIGHT: 4D (77) HEX DEC   Keypress HEX DEC Keypress 01 (01) [ESC] 2C (44) Z 02 (02) ! or 1 2D (45) X 03 (03) @ or 2 2E (46) C 04 (04) # or 3 2F (47) V 05 (05) $ or 4 30 (48) B 06 (06) % or 5 31 (49) N 07 (07) ^ or 6 32 (50) M 08 (08) & or 7 33 (51) < or , 09 (09) * or 8 34 (52) > or . 0A (10) ( or 9 35 (53) ? or / 0B (11) ) or 0 36 (54) Right [Shift] 0C (12) _ or - 37 (55) PrntScr or * (number pad) 0D (13) + or = 38 (56) [ALT] 0E (14) [BACKSPACE] 39 (57) [SPACEBAR] 0F (15) [TAB] 3A (58) [CAPSLOCK] 10 (16) Q 3B (59) [F1] 11 (17) W 3C (60) [F2] 12 (18) E 3D (61) [F3] HEX DEC Keypress HEX DEC Keypress 13 (19) R 3E (62) [F4] 14 (20) T 3F (63) [F5] 15 (21) Y 40 (64) [F6] 16 (22) U 41 (65) [F7] 17 (23) I 42 (66) [F8] 18 (24) O 43 (67) [F9] 19 (25) P 44 (68) [F10] 1A (26) { or [ 45 (69) [NUMLOCK] 1B (27) } or ] 46 (70) [SCROLL LOCK] 1C (28) [ENTER] 47 (71) HOME or 7 (number pad) 1D (29) [CTRL] or Pause/Break @ 48 (72) UP arrow or 8 (number pad) 1E (30) A 49 (73) PGUP or 9 (number pad) 1F (31) S 4A (74) - (number pad) 20 (32) D 4B (75) LEFT arrow or 4 (number pad) 21 (33) F 4C (76) 5 (number pad) 22 (34) G 4D (77) RIGHT arrow or 6 (number pad) 23 (35) H 4E (78) + (number pad) HEX DEC Keypress HEX DEC Keypress 24 (36) J 4F (79) END or 1 (number pad) 25 (37) K 50 (80) DOWN arrow or 2 (number pad) 26 (38) L 51 (81) PGDOWN or 3 (number pad) 27 (39) : or ; 52 (82) INS or 0 (number pad) 28 (40) " or ´ 53 (83) DEL or . (number pad) 29 (41) ` 54 (84) Print Screen or Alt-SysRq? 2A (42) Left [Shift] 57 (87) [F11] 2B (43) | or \ 58 (88) [F12] @ Note: (Pause/Break may lock code returns)

Example: Function to retrieve scancode (useful to detect if more than one key is pressed at once)

DECLARE FUNCTION(code%) 'NOT required in QB64 DIM SHARED keyflags%(0 TO 127) 'the keyboard states CLS ' program or game code DO IF scankey%(1) THEN SYSTEM 'escape exit IF scankey%(72) THEN LOCATE 2, 5: PRINT " UP " ELSE LOCATE 2, 5: PRINT "----" IF scankey%(80) THEN LOCATE 6, 5: PRINT "DOWN" ELSE LOCATE 6, 5: PRINT "----" IF scankey%(75) THEN LOCATE 4, 2: PRINT "LEFT" ELSE LOCATE 4, 2: PRINT "----" IF scankey%(77) THEN LOCATE 4, 8: PRINT "RIGHT" ELSE LOCATE 4, 8: PRINT "---- " LOOP FUNCTION scankey%(scancode%) i% = INP(&H60) IF (i% AND 128) THEN keyflags%(i% XOR 128) = 0 IF (i% AND 128) = 0 THEN keyflags%(i%) = -1 WHILE INKEY$ <> "": WEND scankey% = keyflags%(scancode%) END FUNCTION


UP ---- RIGHT ----

Explanation: Displays multiple arrow key presses to move a game character diagonally. Also allows two players at once.



See also:

INKEY$, ASCII, KEY(n), KEY, Scancodes(examples)


Navigation:
Go to Keyword Reference - Alphabetical
Go to Keyword Reference - By usage
Personal tools