SHARE
TWEET

Fomar0153 - Keyboard Module 1.0

Fomar0153 Jun 23rd, 2012 1,811 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. =begin
  2. Keyboard Module
  3. by Fomar0153
  4. Version 1.0
  5. ----------------------
  6. Notes
  7. ----------------------
  8. Allows you test for keyboard input
  9. ----------------------
  10. Instructions
  11. ----------------------
  12. For a trigger (basically a click, holding a keydown will only trigger
  13. it once) use:
  14. Keyboard.trigger?(Keyboard::VK_KEYA)
  15.  
  16. For a press (basically if the key is down) use:
  17. Keyboard.press?(Keyboard::VK_KEYA)
  18.  
  19. For toggles (press once for on, press again for off) use:
  20. Keyboard.toggle?(Keyboard::VK_KEYA)
  21. ----------------------
  22. Known bugs
  23. ----------------------
  24. None
  25. =end
  26. module Keyboard
  27.  
  28.   KEY_STATE = Win32API.new("user32", "GetKeyState", ["i"], "i")
  29.   AKEY_STATE = Win32API.new("user32", "GetAsyncKeyState", ["i"], "i")
  30.  
  31.   # Key Codes found at:
  32.   # http://msdn.microsoft.com/en-us/library/windows/desktop/dd375731(v=vs.85).aspx
  33.   # Mouse buttons are physical
  34.   VK_LBUTTON  = 0x01  # Left Mouse Button
  35.   VK_RBUTTON  = 0x02  # Right Mouse Button
  36.   VK_CANCEL   = 0x03  # Control-break processing
  37.   VK_MBUTTON  = 0x04  # Middle mouse button (three-button mouse)
  38.   VK_XBUTTON1 = 0x05  # X1 mouse button
  39.   VK_XBUTTON2 = 0x06  # X2 mouse button
  40.   VK_BACK     = 0x08  # BACKSPACE key
  41.   VK_TAB      = 0x09  # TAB key
  42.   VK_CLEAR    = 0x0C  # CLEAR key
  43.   VK_RETURN   = 0x0D  # ENTER key
  44.   VK_SHIFT    = 0x10  # SHIFT key
  45.   VK_CONTROL  = 0x11  # CTRL key
  46.   VK_MENU     = 0x12  # ALT key
  47.   VK_PAUSE    = 0x13  # PAUSE key
  48.   VK_CAPITAL  = 0x14  # CAPS LOCK key
  49.   VK_ESCAPE   = 0x1B  # ESC key
  50.   VK_SPACE    = 0x20  # SPACEBAR
  51.   VK_PRIOR    = 0x21  # PAGE UP key
  52.   VK_NEXT     = 0x22  # PAGE DOWN key
  53.   VK_END      = 0x23  # END key
  54.   VK_HOME     = 0x24  # HOME key
  55.   VK_LEFT     = 0x25  # LEFT ARROW key
  56.   VK_UP       = 0x26  # UP ARROW key
  57.   VK_RIGHT    = 0x27  # RIGHT ARROW key
  58.   VK_DOWN     = 0x28  # DOWN ARROW key
  59.   VK_SELECT   = 0x29  # SELECT key
  60.   VK_PRINT    = 0x2A  # PRINT key
  61.   VK_EXECUTE  = 0x2B  # EXECUTE key
  62.   VK_SNAPSHOT = 0x2C  # PRINT SCREEN key
  63.   VK_INSERT   = 0x2D  # INS key
  64.   VK_DELETE   = 0x2E  # DEL key
  65.   VK_HELP     = 0x2F  # HELP key
  66.   VK_NUMBERS0 = 0x30  # 0 key
  67.   VK_NUMBERS1 = 0x31  # 1 key
  68.   VK_NUMBERS2 = 0x32  # 2 key
  69.   VK_NUMBERS3 = 0x33  # 3 key
  70.   VK_NUMBERS4 = 0x34  # 4 key
  71.   VK_NUMBERS5 = 0x35  # 5 key
  72.   VK_NUMBERS6 = 0x36  # 6 key
  73.   VK_NUMBERS7 = 0x37  # 7 key
  74.   VK_NUMBERS8 = 0x38  # 8 key
  75.   VK_NUMBERS9 = 0x39  # 9 key
  76.   VK_KEYA     = 0x41  # A key
  77.   VK_KEYB     = 0x42  # B key
  78.   VK_KEYC     = 0x43  # C key
  79.   VK_KEYD     = 0x44  # D key
  80.   VK_KEYE     = 0x45  # E key
  81.   VK_KEYF     = 0x46  # F key
  82.   VK_KEYG     = 0x47  # G key
  83.   VK_KEYH     = 0x48  # H key
  84.   VK_KEYI     = 0x49  # I key
  85.   VK_KEYJ     = 0x4A  # J key
  86.   VK_KEYK     = 0x4B  # K key
  87.   VK_KEYL     = 0x4C  # L key
  88.   VK_KEYM     = 0x4D  # M key
  89.   VK_KEYN     = 0x4E  # N key
  90.   VK_KEYO     = 0x4F  # O key
  91.   VK_KEYP     = 0x50  # P key
  92.   VK_KEYQ     = 0x51  # Q key
  93.   VK_KEYR     = 0x52  # R key
  94.   VK_KEYS     = 0x53  # S key
  95.   VK_KEYT     = 0x54  # T key
  96.   VK_KEYU     = 0x55  # U key
  97.   VK_KEYV     = 0x56  # V key
  98.   VK_KEYW     = 0x57  # W key
  99.   VK_KEYX     = 0x58  # X key
  100.   VK_KEYY     = 0x59  # Y key
  101.   VK_KEYZ     = 0x5A  # Z key
  102.  
  103.   def self.trigger?(key)
  104.     if AKEY_STATE.call(key) != 0
  105.       return true
  106.     end
  107.   end
  108.  
  109.   def self.press?(key)
  110.     r = KEY_STATE.call(key)
  111.     if r == -127 or r == -128
  112.       return true
  113.     end
  114.   end
  115.  
  116.   # For things like caps lock
  117.   def self.toggle?(key)
  118.     if  KEY_STATE.call(key) == 1
  119.       return true
  120.     end
  121.   end
  122. end
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top