Ooops
November 25, 2020, 05:29:48 AM

Author Topic: [bb] Windows-style Keyboard Interface by octothorpe [ 1+ years ago ]  (Read 582 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
Title : Windows-style Keyboard Interface
Author : octothorpe
Posted : 1+ years ago

Description : If you want to see an example, copy the program below, then uncomment the short "example" program at the very bottom.

To use this in your program, Include() this library, and call at least one of the "register" routines, then call KEY_GetKey() just like you'd use Blitz's builtin GetKey().

Warning: if you don't call any of the "register" routines, KEY_GetKey() will never return a keystroke!

Differences from how Windows handles the keyboard:

1. If the user presses an unrecognized key while holding down a recognized one, the repeating effect will not be stopped.  To fix this, register each unused key on the keyboard with an ASCII value of 0.

2. Windows allows you to release shift and continue repeating, but pressing shift will stop repeating.  This function differs from Windows's behaviour in that it does not stop repeating if shift is pressed (or released, for that matter).  Emulating this quirk would be easy, but I think it's stupid so I didn't.


Code :
Code: BlitzBasic
  1. Type KEY_Global
  2.         Field scancode_to_ascii.KEY_info[255]
  3.         Field repeat_initial_delay
  4.         Field repeat_subsequent_delay
  5.         Field repeat_timer
  6.         Field depressed_key.KEY_info
  7. End Type
  8. Global KEY_Global.KEY_Global = New KEY_Global
  9. KEY_Global
  10. epeat_initial_delay     = 750
  11. KEY_Global
  12. epeat_subsequent_delay  = 75
  13.  
  14. Type KEY_info
  15.         Field scancode
  16.         Field ascii_plain     ; lowercase
  17.         Field ascii_shift     ; uppercase
  18. End Type
  19.  
  20. ;--------------------------------------------------------
  21. Function KEY_register_keyinfo(scancode, ascii_plain=0, ascii_shift=0)
  22. ;--------------------------------------------------------
  23.         KEY_Globalscancode_to_ascii[scancode] = New KEY_info
  24.         KEY_Globalscancode_to_ascii[scancode]scancode    = scancode
  25.         KEY_Globalscancode_to_ascii[scancode]ascii_plain = ascii_plain
  26.         KEY_Globalscancode_to_ascii[scancode]ascii_shift = ascii_shift
  27. End Function
  28.  
  29. ;--------------------------------------------------------
  30. Function KEY_register_printable_keys()
  31. ;--------------------------------------------------------
  32.         KEY_register_keyinfo( 41,  96, 126)    ; ` and ~
  33.         KEY_register_keyinfo(  2,  49,  33)    ; 1 and !
  34.         KEY_register_keyinfo(  3,  50,  64)    ; 2 and @
  35.         KEY_register_keyinfo(  4,  51,  35)    ; 3 and #
  36.         KEY_register_keyinfo(  5,  52,  36)    ; 4 and $
  37.         KEY_register_keyinfo(  6,  53,  37)    ; 5 and %
  38.         KEY_register_keyinfo(  7,  54,  94)    ; 6 and ^
  39.         KEY_register_keyinfo(  8,  55,  38)    ; 7 and &
  40.         KEY_register_keyinfo(  9,  56,  42)    ; 8 and *
  41.         KEY_register_keyinfo( 10,  57,  40)    ; 9 and (
  42.         KEY_register_keyinfo( 11,  48,  41)    ; 0 and )
  43.         KEY_register_keyinfo( 12,  45,  95)    ; - and _
  44.         KEY_register_keyinfo( 13,  61,  43)    ; = and +
  45.         KEY_register_keyinfo( 16, 113,  81)    ; q and Q
  46.         KEY_register_keyinfo( 17, 119,  87)    ; w and W
  47.         KEY_register_keyinfo( 18, 101,  69)    ; e and E
  48.         KEY_register_keyinfo( 19, 114,  82)    ; r and R
  49.         KEY_register_keyinfo( 20, 116,  84)    ; t and T
  50.         KEY_register_keyinfo( 21, 121,  89)    ; y and Y
  51.         KEY_register_keyinfo( 22, 117,  85)    ; u and U
  52.         KEY_register_keyinfo( 23, 105,  73)    ; i and I
  53.         KEY_register_keyinfo( 24, 111,  79)    ; o and O
  54.         KEY_register_keyinfo( 25, 112,  80)    ; p and P
  55.         KEY_register_keyinfo( 26,  91, 123)    ; [ and {
  56.         KEY_register_keyinfo( 27,  93, 125)    ; ] and }
  57.         KEY_register_keyinfo( 43,  92, 124)    ;  and |
  58.         KEY_register_keyinfo( 30,  97,  65)    ; a and A
  59.         KEY_register_keyinfo( 31, 115,  83)    ; s and S
  60.         KEY_register_keyinfo( 32, 100,  68)    ; d and D
  61.         KEY_register_keyinfo( 33, 102,  70)    ; f and F
  62.         KEY_register_keyinfo( 34, 103,  71)    ; g and G
  63.         KEY_register_keyinfo( 35, 104,  72)    ; h and H
  64.         KEY_register_keyinfo( 36, 106,  74)    ; j and J
  65.         KEY_register_keyinfo( 37, 107,  75)    ; k and K
  66.         KEY_register_keyinfo( 38, 108,  76)    ; l and L
  67.         KEY_register_keyinfo( 39,  59,  58)    ; ; and :
  68.         KEY_register_keyinfo( 40,  39,  34)    ; ' and "
  69.         KEY_register_keyinfo( 44, 122,  90)    ; z and Z
  70.         KEY_register_keyinfo( 45, 120,  88)    ; x and X
  71.         KEY_register_keyinfo( 46,  99,  67)    ; c and C
  72.         KEY_register_keyinfo( 47, 118,  86)    ; v and V
  73.         KEY_register_keyinfo( 48,  98,  66)    ; b and B
  74.         KEY_register_keyinfo( 49, 110,  78)    ; n and N
  75.         KEY_register_keyinfo( 50, 109,  77)    ; m and M
  76.         KEY_register_keyinfo( 51,  44,  60)    ; , and <
  77.         KEY_register_keyinfo( 52,  46,  62)    ; . and >
  78.         KEY_register_keyinfo( 53,  47,  63)    ; / and ?
  79.         KEY_register_keyinfo( 15,   9,   9)    ; tab
  80.         KEY_register_keyinfo( 57,  32,  32)    ; space
  81.         KEY_register_keyinfo( 28,  13,  13)    ; enter
  82.         KEY_register_keyinfo(181,  47,  47)    ; numpad /
  83.         KEY_register_keyinfo( 55,  42,  42)    ; numpad *
  84.         KEY_register_keyinfo( 74,  45,  45)    ; numpad -
  85.         KEY_register_keyinfo( 78,  43,  43)    ; numpad +
  86.         KEY_register_keyinfo(156,  13,  13)    ; numpad enter
  87.         KEY_register_keyinfo( 79,  49,  49)    ; numpad 1
  88.         KEY_register_keyinfo( 80,  50,  50)    ; numpad 2
  89.         KEY_register_keyinfo( 81,  51,  51)    ; numpad 3
  90.         KEY_register_keyinfo( 75,  52,  52)    ; numpad 4
  91.         KEY_register_keyinfo( 76,  53,  53)    ; numpad 5
  92.         KEY_register_keyinfo( 77,  54,  54)    ; numpad 6
  93.         KEY_register_keyinfo( 71,  55,  55)    ; numpad 7
  94.         KEY_register_keyinfo( 72,  56,  56)    ; numpad 8
  95.         KEY_register_keyinfo( 73,  57,  57)    ; numpad 9
  96.         KEY_register_keyinfo( 82,  48,  48)    ; numpad 0
  97.         KEY_register_keyinfo( 83,  46,  46)    ; numpad .
  98. End Function
  99.  
  100. ;--------------------------------------------------------
  101. Function KEY_register_manipulation_keys()
  102. ;--------------------------------------------------------
  103.         KEY_register_keyinfo(199,  1,  1)     ; home
  104.         KEY_register_keyinfo(207,  2,  2)     ; end
  105.         KEY_register_keyinfo( 14,  8,  8)     ; backspace
  106.         KEY_register_keyinfo(211,  4,  4)     ; delete
  107.         KEY_register_keyinfo(203, 31, 31)     ; left
  108.         KEY_register_keyinfo(205, 30, 30)     ; right
  109. End Function
  110.  
  111. ;--------------------------------------------------------
  112. Function KEY_GetKey()
  113. ;--------------------------------------------------------
  114.  
  115.         Local trigger_keystroke = False
  116.  
  117.         ; is the user still pressing the previous key?
  118.         If KEY_Globaldepressed_key <> Null Then
  119.                 If KeyDown(KEY_Globaldepressed_keyscancode) Then
  120.                         ; user is still pressing the key, so generate repeating keystrokes
  121.                         If KEY_Global
  122. epeat_timer < MilliSecs() Then
  123.                                 KEY_Global
  124. epeat_timer = MilliSecs() + KEY_Global
  125. epeat_subsequent_delay
  126.                                 trigger_keystroke = True
  127.                         EndIf
  128.                 Else
  129.                         ; user has stopped pressing the key
  130.                         KEY_Globaldepressed_key = Null
  131.                 EndIf
  132.         EndIf
  133.  
  134.         ; has the user pressed a new key? (check the key positions for each recognized scancode)
  135.         For key.KEY_info = Each KEY_info
  136.                 If KeyHit(keyscancode) Then Exit
  137.         Next
  138.         ; if we found a recognized key, remember it's being pressed, and return it
  139.         If key <> Null Then
  140.                 KEY_Globaldepressed_key = key
  141.                 KEY_Global
  142. epeat_timer = MilliSecs() + KEY_Global
  143. epeat_initial_delay
  144.                 trigger_keystroke = True
  145.         EndIf
  146.  
  147.         If trigger_keystroke Then
  148.                 ; the ascii code we want to return often depends on if shift is being pressed
  149.                 If KeyDown(42) Or KeyDown(54) Then
  150.                         Return KEY_Globaldepressed_keyascii_shift
  151.                 Else
  152.                         Return KEY_Globaldepressed_keyascii_plain
  153.                 EndIf
  154.         EndIf
  155.  
  156.         Return 0
  157. End Function
  158.  
  159.  
  160. ;--------------------------------------------------------
  161. ; EXAMPLE
  162. ;--------------------------------------------------------
  163. ;
  164. ;SetBuffer = BackBuffer()
  165. ;
  166. ;KEY_register_printable_keys()
  167. ;
  168. ;input_string$ = ""
  169. ;While Not KeyHit(1)
  170. ;       char = KEY_GetKey()
  171. ;       If char > 0 Then input_string$ = input_string$ + Chr$(char)
  172. ;
  173. ;       Cls
  174. ;       Locate 1, 1
  175. ;       Print "Press and hold keys to see repeating effect"
  176. ;       Print input_string$ + cursor$
  177. ;       Flip
  178. ;Wend


Comments :


mulawa1(Posted 1+ years ago)

 Superb piece of work - worked perfectly for me!Busily writing an interactive character-by-character peer-to-peer chat program and this was just what I needed - able to plug it straight in!Many thanks.


 

SimplePortal 2.3.6 © 2008-2014, SimplePortal