* '''Keyboards with Alt Gr key:''' [[_KEYHIT]] may return both Alt(100307) and Ctrl(100306) codes when that key is pressed or released.
* '''Linux with foreign keyboards:''' [[SHELL]] [[_HIDE]] "setxkbmap us" will setup a keyboard to read US [[Scancodes]].


==INKEY$ Codes==
* [[INKEY$]] returns a [[STRING]] [[ASCII]] character or a function key two byte string. [[ASC]] converts to code. Use: '''{{text|key$ &lt;nowiki>=&lt;/nowiki> INKEY$|green}}'''
* Key presses are read consecutively from the keyboard buffer which will retain a [[SLEEP]] key press.

{{WhiteStart}}'                                '''ASCII Keyboard Codes'''
'
'''' Esc  F1  F2  F3  F4  F5  F6  F7  F8  F9  F10  F11  F12  Sys ScL Pause'''                  
'  27 +59 +60 +61 +62 +63 +64 +65 +66 +67 +68  +133 +134   -   -    -
'''' `~  1!  2@  3#  4$  5%  6^  7&amp;  8*  9(  0) -_ =+ BkSp   Ins Hme PUp   NumL  /   *    -''' 
' 126 33  64  35  36  37  94  38  42  40  41 95 43   8    +82 +71 +73    -    47  42   45
'''  96 49  50  51  52  53  54  55  56  57  48 45 61''
'''' Tab Q   W   E   R   T   Y   U   I   O   P  [{  ]}  \|   Del End PDn   7Hme 8/▲  9PU  + '''
'  9  81  87  69  82  84  89  85  73  79  80 123 125 124  +83 +79 +81   +71  +72  +73  43
'''    113 119 101 114 116 121 117 105 111 112  91  93  92                 55   56   57 ''
'''' CapL  A   S   D   F   G   H   J   K   L   ;:  '" Enter                4/◄-  5   6/-►  
'   -   65  83  68  70  71  72  74  75  76  58  34  13                  +75  +76  +77  '''E'''
'''       97 115 100 102 103 104 106 107 108  59  39                       52   53   54 '' '''n'''                                    
'''' Shift  Z   X   C   V   B   N   M   ,&lt;  .>  /?    Shift       ▲        1End 2/▼  3PD  t'''
'   *    90  88  67  86  66  78  77  60  62  63      *        +72       +79  +80  +81  '''e'''
'''       122 120  99 118  98 110 109  44  46  47                          49   50   51 '' '''r'''
'''' Ctrl Win Alt       Spacebar          Alt Win Menu Ctrl   ◄-  ▼   -►   0Ins     .Del '''
'  *    -   *           32              *   -   -    *    +75 +80 +77   +82       +83  13 
'                                                                   ''     48        46''
'
' '''    ''Italics'' = LCase/NumLock On  [[INKEY$#Two_Byte_Combinations|* = 2 byte combo only]],  + = 2 Byte: CHR$(0) + CHR$(code)'''
'
{{WhiteEnd}}

&lt;center>GO TO: [[Keyboard_scancodes#INP_Scan_Codes|Scan Codes]], 
[[Keyboard_scancodes#ON_KEY_Events|ON KEY]],  [[Keyboard_scancodes#KEYHIT_and_KEYDOWN_Codes|_KEYHIT]], [[Keyboard_scancodes#DEVICES_Button|_DEVICES]], [[Keyboard_scancodes#Windows_Virtual_Codes|Windows Virtual]]&lt;/center>

==INP and _CINP Scan Codes==
* [[_CINP]] can be used in [[$CONSOLE]] Windows to read individual key presses.
* [[INP]]([[&amp;H]]60) returns multiple [[INTEGER]] press and release codes as keys are used. Use: '''{{text|code% &lt;nowiki>=&lt;/nowiki> INP(96)|green}}'''
* To clear the keyboard buffer, use the [[INKEY$]] function before or after the [[INP]] read to avoid buffer overflows and beeps.          
* Keys are assigned only one code irregardless of case or Lock mode with release codes being the press  code + 128.

{{WhiteStart}}'                            '''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&amp; 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  ,&lt; .> /?    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  {{text|91|purple}}  56        57       56  {{text|92   93|purple}}  29       75  80  77       82          83   28 
'
'   {{text|QB64 codes only!|purple}} '''Release codes = Press code + 128. Pause/Break may lock code returns.'''
'
{{WhiteEnd}}

&lt;center>[http://www.quadibloc.com/comp/scan.htm Keyboard Scan Codes]........... [http://www.win.tue.nl/~aeb/linux/kbd/scancodes.html Other Keyboards]&lt;/center>


&lt;center>GO TO:[[Keyboard_scancodes#INKEY.24_Codes|INKEY$]], [[Keyboard_scancodes#ON_KEY_Events|ON KEY]], [[Keyboard_scancodes#KEYHIT_and_KEYDOWN_Codes|_KEYHIT]], [[Keyboard_scancodes#DEVICES_Button|_DEVICES]], [[Keyboard_scancodes#Windows_Virtual_Codes|Windows Virtual]]&lt;/center>

==ON KEY Events==

* Assign user defined Soft Key or key combination numbers 15 to 29 with: '''{{text|[[KEY n]]umber, CHR$({{text|keyflag|purple}}) + CHR$(scancode)|green}}'''
* Combo Key flags: {{text|&lt;nowiki>0 = none, LShift = 1, RShift = 2, LCtrl = 4, LAlt = 8, NumLock = 32, CapsLock = 64&lt;/nowiki>|purple}}, {{text|Extended keys &lt;nowiki>=&lt;/nowiki> 128|blue}}
* {{text|Red|red}} KEY numbers {{text|1 to 14, 30 or 31|red}} assign predefined one key events using: '''[[ON KEY(n)|ON KEY]]({{text|number|red}}) [[GOSUB]] {{text|subname|green}}'''

{{WhiteStart}}'                           '''Soft Key Scan Code Values'''
'
'       {{text|1  2  3  4  5  6  7  8  9  10   30  31                       Predefined Keys|red}}
''''  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     --    70    29            
'  '''`~  1! 2@ 3# 4$ 5% 6^ 7&amp; 8* 9( 0) -_ =+ BkSpc  Insert Home PgUp   NumL   /     *    -''' 
'   41 2  3  4  5  6  7  8  9  10 11 12 13  14     {{text|82    71    73|blue}}    {{text|32|purple}}/69  {{text|53|blue}}    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     {{text|83    79    81|blue}}     71   {{text|11|red}}/72  73   78
'  '''CapL  A  S  D  F  G  H  J  K  L  ;: '"  Enter                     4/◄-   5    6/-►  E'''
'  {{text|64|purple}}/58 30 31 32 33 34 35 36 37 38 39 40   28                       {{text|12|red}}/75  76   {{text|13|red}}/77 '''n'''
'  '''Shift  Z  X  C  V  B  N  M  ,&lt; .> /?    Shift         ▲           1/End  2/▼  3/PD  t'''
'  {{text|1|purple}}/42   44 45 46 47 48 49 50 51 52 53    {{text|2|purple}}/54          {{text|72|blue}}           79   {{text|14|red}}/80  81   '''e'''
'  '''Ctrl Win Alt    Spacebar    Alt Win Menu Ctrl     ◄-  ▼   -►      0/Insert    ./Del r'''
'  {{text|4|purple}}/29  {{text|91|orange}} {{text|8|purple}}/56      57       {{text|56|blue}}  {{text|92   93|orange}}  {{text|29       75  80  77|blue}}       82          83   {{text|28|blue}} 
'
'   '''Keyflag:''' {{text|Function(0, 1, 2, 4, 8, 32, 64)|purple}}, {{text|Extended(128)|blue}} '''Scan Code: '''1-88, {{text|QB64 only(91-93)|orange}}
'
'        Reserved and function key combinations can be made using the scan code instead.
'             Add function and/or extended flag values for more key combinations.
{{WhiteEnd}}

&lt;center>GO TO:[[Keyboard_scancodes#INKEY.24_Codes|INKEY$]], [[Keyboard_scancodes#INP_Scan_Codes|Scan Codes]], [[Keyboard_scancodes#KEYHIT_and_KEYDOWN_Codes|_KEYHIT]], [[Keyboard_scancodes#DEVICES_Button|_DEVICES]], [[Keyboard_scancodes#Windows_Virtual_Codes|Windows Virtual]]&lt;/center>

==KEYHIT and KEYDOWN Codes==
* [[_KEYHIT]] returns multiple [[LONG]] press and negative release codes as keys are used. Use: '''{{text|code&amp; &lt;nowiki>=&lt;/nowiki> _KEYHIT|green}}'''
* [[_KEYDOWN]] returns -1 when a specified key code value is pressed or 0. Use: '''{{text|status% &lt;nowiki>=&lt;/nowiki> _KEYDOWN(code&amp;)|green}}'''
* [[_KEYCLEAR]] clears the keyboard buffer memory instantly without a [[LOOP|loop]].

{{WhiteStart}}'                            '''QB64 _KEYHIT and _KEYDOWN Values'''
'
''''Esc  F1    F2    F3    F4    F5    F6    F7    F8    F9    F10   F11   F12   Sys  ScL Pause'''                  
' 27 15104 15360 15616 15872 16128 16384 16640 16896 17152 17408 34048 34304 +316 +302 +019
''''`~  1!  2@  3#  4$  5%  6^  7&amp;  8*  9(  0) -_ =+ BkSp   Ins   Hme   PUp   NumL   /     *    -''' 
'126 33  64  35  36  37  94  38  42  40  41 95 43   8   20992 18176 18688 +300   47    42   45
''' 96 49  50  51  52  53  54  55  56  57  48 45 61''
''''Tab Q   W   E   R   T   Y   U   I   O   P  [{  ]}  \|   Del   End   PDn   7Hme  8/▲   9PU   + '''
' 9  81  87  69  82  84  89  85  73  79  80 123 125 124 21248 20224 20736 18176 18432 18688 43
'''   113 119 101 114 116 121 117 105 111 112  91  93  92                    55    56    57 ''
''''CapL   A   S   D   F   G   H   J   K   L   ;:  '" Enter                   4/◄-   5    6/-►  
'+301   65  83  68  70  71  72  74  75  76  58  34  13                    19200 19456 19712  '''E'''
'''       97 115 100 102 103 104 106 107 108  59  39                         52    53    54 ''   '''n'''                                    
''''Shift   Z   X   C   V   B   N   M   ,&lt;  .>  /?    Shift       ▲           1End  2/▼   3PD   t'''
'+304   90  88  67  86  66  78  77  60  62  63    +303       18432        20224 20480 20736  '''e'''
'''      122 120  99 118  98 110 109  44  46  47                             49    50    51 ''   '''r'''
''''Ctrl   Win  Alt     Spacebar      Alt  Win  Menu  Ctrl   ◄-   ▼   -►      0Ins        .Del '''
'+306  +311 +308       32         +307 +312 +319  +305 19200 20480 19712  20992       21248 13 
'                                                                     ''     48          46''
'     '''    Lower value = LCase/NumLock On __________________ + = add 100000 '''
{{WhiteEnd}}

&lt;center>GO TO: [[Keyboard_scancodes#INKEY.24_Codes|INKEY$]], [[Keyboard_scancodes#INP_Scan_Codes|Scan Codes]], [[Keyboard_scancodes#ON_KEY_Events|ON KEY]], [[Keyboard_scancodes#DEVICES_Button|_DEVICES]], [[Keyboard_scancodes#Windows_Virtual_Codes|Windows Virtual]]&lt;/center>

: Most keys use [[ASCII]] key code values. Function key code values can be calculated using the [[INKEY$]], two byte values with [[CVI]].
&lt;center>Example: '''{{text|status &lt;nowiki>=&lt;/nowiki> _KEYDOWN(CVI(CHR$(0) + "P"))|green}}'''&lt;/center>

&lt;center>[[INKEY$#Two_Byte_Combinations|For Ctrl, Alt, Shift Key Combinations see Two Byte Codes]]&lt;/center>

==[[DEVICES]] Button==

* [[_LASTBUTTON]](1) keyboards will normally return 512 buttons. One button is read per loop through all numbers.
* [[_BUTTONCHANGE]](number) returns -1 when pressed, 1 when released and 0 when there is no event since the last read.
* [[_BUTTON]](number) returns -1 when a button is pressed and 0 when released.


{{WhiteStart}}'                              '''Keyboard Device 1 Button Numbers'''
'
''''  Esc F1  F2  F3  F4  F5  F6  F7  F8  F9  F10  F11 F12      SysReq ScrL Pause'''                  
'  177 436 433 434 439 440 437 438 395 396 393  394 399       402   392   ---
'  '''`~  1!  2@  3#  4$  5%  6^  7&amp;  8*  9(  0)  -_  =+ BkSpc  Insert Home PgUp   NumL    /    *    -''' 
'  246 155 156 153 154 159 160 157 158 147 134 135 151 174    447   448   446   386    417  418  423
'  '''Tab  Q   W   E   R   T   Y   U   I   O   P   [{  ]}  \|   Delete End  PgDn   7/Home 8/▲  9/PU  + '''
'  163 219 221 207 220 218 211 223 195 197 198 241 247 242    213   445   435   429    430  419  424
'  '''CapL  A   S   D   F   G   H   J   K   L   ;:  '"   Enter                     4/◄-    5   6/-►  E'''
'  391  203 217 202 208 205 206 196 193 194  145 141   167                      426    431  432   '''n'''
'  '''Shift  Z   X   C   V   B   N   M  ,&lt;  .>  /?       Shift          ▲          1/End  2/▼  3/PD  t'''
'  390   212 222 201 224 204 200 199 130 136 133       389          443         427    428  432   '''e'''
'  '''Ctrl Win  Alt    Spacebar    Alt  Win  Menu Ctrl            ◄-    ▼    -►    0/Insert    ./Del r'''
'  412  413  410      182       409  414  405  411             442  444   441    86         420  421 
'
'                        '''Mouse Device 2 buttons: Left = 1, Middle = 2, Right = 3'''
{{WhiteEnd}}


&lt;center>GO TO: [[Keyboard_scancodes#INKEY.24_Codes|INKEY$]], [[Keyboard_scancodes#INP_Scan_Codes|Scan Codes]], [[Keyboard_scancodes#ON_KEY_Events|ON KEY]], [[Keyboard_scancodes#KEYHIT_and_KEYDOWN_Codes|_KEYHIT]], 
[[Keyboard_scancodes#Windows_Virtual_Codes|Windows Virtual]]&lt;/center>

==Windows Virtual Codes==

* Virtual key code status can be read '''independently of program focus in Windows'''. Not available in Linux or Mac systems.
* Use: '''{{text|status &lt;nowiki>=&lt;/nowiki> GetKeyState%(code%)|green}}''' or '''{{text|GetAsyncKeyState%(code&amp;)|green}}''' or with '''{{text|keybd_event|green}}''' in [[Windows_Libraries#Hot_Keys_.28maximize.29|DECLARE LIBRARY]] "User32".


{{WhiteStart}}'                                  '''Virtual KeyState Codes'''
'
'''' Esc  F1  F2  F3  F4  F5  F6  F7  F8  F9  F10  F11 F12   Sys ScL Pause'''                  
' 27   112 113 114 115 116 117 118 119 120 121  122 123   44  145 19
'''' `~  1!  2@  3#  4$  5%  6^  7&amp;  8* 9(  0) -_  =+  BkS   Ins Hme PUp   NumL  /   *    -''' 
' 192 49  50  51  52  53  54  55  56  57 48 189 187  8    45  36  33    144  111  106 109
'''' Tab Q   W   E   R   T   Y   U   I   O   P  [{  ]}  \|   Del End PDn   7Hme 8/▲  9PU  +'''
'  9  81  87  69  82  84  89  85  73  79  80 219 221 120  46  35  34    103  104  105 107
'''' CapL  A   S   D   F   G   H   J   K   L   ;:  '" Enter                4/◄-  5   6/-► E'''
'  20   65  83  68  70  71  72  74  75  76 186 222  13                  100  101  102  '''n'''                                                                                                                          
'''' Shift  Z   X   C   V   B   N   M   ,&lt;  .>  /?    Shift       ▲        1End 2/▼  3PD  t'''
' 16/160 90  88  67  86  66  78  77 188  190 191   16/161      38       97   98   99   '''e'''                                                                                       
'''' Ctrl   Win   Alt       Spacebar      Alt Win Menu Ctrl   ◄-  ▼   -►   0Ins     .Del  r'''
' 17/162 91  18/164        32        18/165 92  93 17/163  37  40  39   96        110  13
'
'     Num Lock On values shown. Off values same as functions and arrows, 5 = code 12.
'
'  '''Mouse click returns: LB = 1, RB = 2, MB = 4, etc. [http://msdn.microsoft.com/en-us/library/windows/desktop/dd375731(v=vs.85).aspx Special keys] may also work.'''
{{WhiteEnd}}


&lt;center>GO TO: [[Keyboard_scancodes#INKEY.24_Codes|INKEY$]], [[Keyboard_scancodes#INP_Scan_Codes|Scan Codes]], [[Keyboard_scancodes#ON_KEY_Events|ON KEY]], [[Keyboard_scancodes#KEYHIT_and_KEYDOWN_Codes|_KEYHIT]], [[Keyboard_scancodes#DEVICES_Button|_DEVICES]], &lt;/center>

==Example Code==
''Example 1:'' An [[INP]] Function that retrieves multiple scancodes (allows 2 players and diagonal moves). Function includes it's own array.

{{CodeStart}} '' ''
{{Cl|CLS}}
x = 40: px = x
y = 20: py = y
xx = 41: pxx = xx
yy = 21: pyy = yy

{{Cl|DO...LOOP|DO}} ' program or game loop
  {{Cl|_LIMIT}} 20  ' Delay .05 'in QBasic
  {{Cl|COLOR}} 15
  {{Cl|LOCATE}} 8, 14: {{Cl|PRINT}} "W A S D": {{Cl|LOCATE}} 8, 52: {{Cl|PRINT}} "ARROW PAD"
  {{Cl|IF...THEN|IF}} ScanKey%(17) {{Cl|THEN}} 'W key
    {{Cl|LOCATE}} 2, 15: {{Cl|PRINT}} " UP "
    {{Cl|IF...THEN|IF}} y > 1 {{Cl|THEN}} y = y - 1
  {{Cl|ELSE}} {{Cl|LOCATE}} 2, 15: {{Cl|PRINT}} "----" 
  {{Cl|END IF}}
  {{Cl|IF...THEN|IF}} ScanKey%(31) {{Cl|THEN}} 'S key
    {{Cl|LOCATE}} 6, 15: {{Cl|PRINT}} "DOWN"
    {{Cl|IF...THEN|IF}} y &lt; 25 {{Cl|THEN}} y = y + 1
  {{Cl|ELSE}} {{Cl|LOCATE}} 6, 15: {{Cl|PRINT}} "----" 
  {{Cl|END IF}}
  {{Cl|IF...THEN|IF}} ScanKey%(30) {{Cl|THEN}} 'A key
    {{Cl|LOCATE}} 4, 12: {{Cl|PRINT}} "LEFT"
    {{Cl|IF...THEN|IF}} x > 1 {{Cl|THEN}} x = x - 1
  {{Cl|ELSE}} {{Cl|LOCATE}} 4, 12: {{Cl|PRINT}} "----" 
  {{Cl|END IF}}
  {{Cl|IF...THEN|IF}} ScanKey%(32) {{Cl|THEN}} 'D key
    {{Cl|LOCATE}} 4, 18: {{Cl|PRINT}} "RIGHT"
    {{Cl|IF...THEN|IF}} x &lt; 80 {{Cl|THEN}} x = x + 1
  {{Cl|ELSE}} {{Cl|LOCATE}} 4, 18: {{Cl|PRINT}} "---- " 
  {{Cl|END IF}}
  {{Cl|IF...THEN|IF}} ScanKey%(72) {{Cl|THEN}} 'up arrow
    {{Cl|LOCATE}} 2, 55: {{Cl|PRINT}} " UP " 
    {{Cl|IF...THEN|IF}} yy > 1 {{Cl|THEN}} yy = yy - 1
  {{Cl|ELSE}} {{Cl|LOCATE}} 2, 55: {{Cl|PRINT}} "----" 
  {{Cl|END IF}}
  {{Cl|IF...THEN|IF}} ScanKey%(80) {{Cl|THEN}} 'down arrow
    {{Cl|LOCATE}} 6, 55: {{Cl|PRINT}} "DOWN"  
    {{Cl|IF...THEN|IF}} yy &lt; 25 {{Cl|THEN}} yy = yy + 1
  {{Cl|ELSE}} {{Cl|LOCATE}} 6, 55: {{Cl|PRINT}} "----" 
  {{Cl|END IF}}
  {{Cl|IF...THEN|IF}} ScanKey%(75) {{Cl|THEN}} 'left arrow
    {{Cl|LOCATE}} 4, 52: {{Cl|PRINT}} "LEFT" 
    {{Cl|IF...THEN|IF}} xx > 1 {{Cl|THEN}} xx = xx - 1
  {{Cl|ELSE}} {{Cl|LOCATE}} 4, 52: {{Cl|PRINT}} "----" 
  {{Cl|END IF}}
  {{Cl|IF...THEN|IF}} ScanKey%(77) {{Cl|THEN}} 'right arrow
    {{Cl|LOCATE}} 4, 58: {{Cl|PRINT}} "RIGHT" 
    {{Cl|IF...THEN|IF}} xx &lt; 80 {{Cl|THEN}} xx = xx + 1
  {{Cl|ELSE}} {{Cl|LOCATE}} 4, 58: {{Cl|PRINT}} "---- " 
  {{Cl|END IF}}
  {{Cl|LOCATE}} py, px: {{Cl|PRINT}} {{Cl|SPACE$}}(1);  'erase sprite at previous position
  {{Cl|LOCATE}} pyy, pxx: {{Cl|PRINT}} {{Cl|SPACE$}}(1);
  {{Cl|COLOR}} 10: {{Cl|LOCATE}} y, x: {{Cl|PRINT}} {{Cl|CHR$}}(1); 'place sprite at new position
  {{Cl|COLOR}} 12: {{Cl|LOCATE}} yy, xx: {{Cl|PRINT}} {{Cl|CHR$}}(2);
  px = x: py = y: pxx = xx: pyy = yy
{{Cl|LOOP}} {{Cl|UNTIL}} ScanKey%(1)
zerocodes% = ScanKey%(0) 'reset all array values to zero for next part of program
{{Cl|END}}

{{Cl|FUNCTION}} ScanKey% (scancode%)
{{Cl|STATIC}} Ready%, keyflags%() 'retain values on procedure exit
{{Cl|IF...THEN|IF}} {{Cl|NOT}} Ready% {{Cl|THEN}} {{Cl|REDIM}} keyflags%(0 {{Cl|TO}} 127): Ready% = -1 'create array on first use
i% = {{Cl|INP}}({{Cl|&amp;H}}60) 'read keyboard states
{{Cl|IF...THEN|IF}} (i% {{Cl|AND (boolean)|AND}} 128) {{Cl|THEN}} keyflags%(i% {{Cl|XOR (boolean)|XOR}} 128) = 0
{{Cl|IF...THEN|IF}} (i% {{Cl|AND (boolean)|AND}} 128) = 0 {{Cl|THEN}} keyflags%(i%) = -1
K$ = {{Cl|INKEY$}} 'clears the key from buffer to prevent beeps
ScanKey% = keyflags%(scancode%)
{{Cl|IF...THEN|IF}} scancode% = 0 {{Cl|THEN}} Ready% = 0 'a zero scancode value clears all previous key presses with a REDIM
{{Cl|END FUNCTION}}

{{Cl|SUB}} Delay (dlay!) 'optional QBasic delay
start! = {{Cl|TIMER}}
{{Cl|DO...LOOP|DO}} {{Cl|WHILE}} start! + dlay! >= {{Cl|TIMER}}
  {{Cl|IF...THEN|IF}} start! > {{Cl|TIMER}} {{Cl|THEN}} start! = start! - 86400
{{Cl|LOOP}}
{{Cl|END SUB}} '' ''
{{CodeEnd}}
{{OutputStart}}     
                UP                                      UP

            ----  RIGHT                             LEFT  ----

               ----                                    ----    

             W A S D                                ARROW PAD





                                      {{text|☺|#00FF00}}
                                       {{text|☻|#FF0000}} 
{{OutputEnd}}
{{small|Adapted from Code by Cyperium.}}
:''Explanation:'' Displays multiple arrow key presses to move a game character diagonally or allows 2 players to press keys at once. Each keypress is placed into a [[STATIC]] array inside of the ScanKey function. When a key is pressed, the corresponding code index in the array is changed to -1 or True. When a key is released, the scancode returned is over 128 so AND 128 becomes True and the XOR 128 index value is changed to 0. XOR causes 128 to be subtracted from the release code index value which is then changed to 0 (False) from -1. As the main program loop reads the array each of the IF statements look to see if that code is -1. When a statement is True, it prints the appropriate key(s). The example moves two [[ASCII]] sprites. '''Note:''' [[INKEY$]] can be removed from the function if it is used elsewhere to help clear the key buffer. 

&lt;center>'''The Scankey Function array can be cleared by sending 0 as the ''scancode%'' or press the ''Print Scrn/SysReq'' key.'''&lt;/center>


''Example 2:'' How to get Control, Alt and shift key entries from a user using [[PEEK]] in QB64 or QBasic:
{{CodeStart}} '' ''
DO
    {{Cl|_LIMIT}} 50
    {{Cl|DEF SEG}} = {{Cl|&amp;H}}40  'set memory segment to read
    ShiftFlag = {{Cl|PEEK}}({{Cl|&amp;H}}17)  'read keyboard function keys
    {{Cl|DEF SEG}}
    {{Cl|IF...THEN|IF}} ShiftFlag &lt;> x {{Cl|THEN}}  'find key not previously read
        x = ShiftFlag  'read a new keypress
        {{Cl|IF...THEN|IF}} (ShiftFlag {{Cl|AND (boolean)|AND}} 1) = 1 {{Cl|THEN}} {{Cl|PRINT}} "rightshift"
        {{Cl|IF...THEN|IF}} (ShiftFlag {{Cl|AND (boolean)|AND}} 2) = 2 {{Cl|THEN}} {{Cl|PRINT}} "leftshift"
        {{Cl|IF...THEN|IF}} (ShiftFlag {{Cl|AND (boolean)|AND}} 4) = 4 {{Cl|THEN}} {{Cl|PRINT}} "ctrl"
        {{Cl|IF...THEN|IF}} (ShiftFlag {{Cl|AND (boolean)|AND}} 8) = 8 {{Cl|THEN}} {{Cl|PRINT}} "alt"
    {{Cl|END IF}}
    x$ = {{Cl|INKEY$}} 'clear keyboard buffer to prevent beeping
    {{Cl|IF...THEN|IF}} x$ &lt;> "" {{Cl|THEN}} {{Cl|PRINT}} {{Cl|ASC}}(x$)  'read other keypress {{Cl|ASCII}} codes
{{Cl|LOOP}} {{Cl|UNTIL}} x$ = {{Cl|CHR$}}(27) 'Escape key exit
{{CodeEnd}} {{small|Derived from code by eoredson}}
: Note: Each key must be read individually or sequentially. For combinations use the QB64 [[_KEYDOWN]] function.


&lt;p style="text-align: center">([[#toc|Return to Table of Contents]])&lt;/p>

==Reference==

''See also:'' 
* [[INKEY$]], [[ASCII]], [[ASC]] 
* [[_KEYHIT]], [[_KEYDOWN]], [[_KEYCLEAR]]
* [[ON KEY(n)]], [[KEY(n)]], [[KEY]], 
* [[INP]], [[Scancodes]] {{text|(examples)}}
* [[Windows_Libraries#Hot_Keys_.28maximize.29|Windows hot keys]]
* [http://www.howtogeek.com/236462/which-key-on-a-mac-keyboard-corresponds-to-the-symbol/ Mac keyboard Symbols]
 

{{PageNavigation}}
