Advertisement
Omsigames

grapes/Keyboard.lua

Dec 25th, 2024
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.18 KB | None | 0 0
  1. local keyboard = {
  2.   -- Control
  3.   LEFT_CONTROL = 29,
  4.   RIGHT_CONTROL = 157,
  5.   -- Shift
  6.   LEFT_SHIFT = 42,
  7.   RIGHT_SHIFT = 54,
  8.   -- Alt
  9.   LEFT_ALT = 56,
  10.   RIGHT_ALT = 184,
  11.   -- Command (Mac & Linux) or Windows Key (Windows), I added both in case someone is from windows
  12.   COMMAND_KEY = 219,
  13.   WINDOWS_KEY = 219,
  14.  
  15.   -- Numbers (Not from Num Pad)
  16.   ONE = 2,
  17.   TWO = 3,
  18.   THREE = 4,
  19.   FOUR = 5,
  20.   FIVE = 6,
  21.   SIX = 7,
  22.   SEVEN = 8,
  23.   EIGHT = 9,
  24.   NINE = 10,
  25.   ZERO = 11,
  26.  
  27.   -- Letters
  28.   A = 30,
  29.   B = 48,
  30.   C = 46,
  31.   D = 32,
  32.   E = 18,
  33.   F = 33,
  34.   G = 34,
  35.   H = 35,
  36.   I = 23,
  37.   J = 36,
  38.   K = 37,
  39.   L = 38,
  40.   M = 50,
  41.   N = 49,
  42.   O = 24,
  43.   P = 25,
  44.   Q = 16,
  45.   R = 19,
  46.   S = 31,
  47.   T = 20,
  48.   U = 22,
  49.   V = 47,
  50.   W = 17,
  51.   X = 45,
  52.   Y = 21,
  53.   Z = 44,
  54.  
  55.   -- Misc
  56.   MINUS= 12,
  57.   PLUS = 13,
  58.   BACKSPACE = 14,
  59.   TAB = 15,
  60.   SINGLE_QUOTE = 26,
  61.   EXCLAMATION = 27,
  62.   ENTER = 28,
  63.   OPEN_QUOTE = 40,
  64.   COMMA = 51,
  65.   DOT = 52,
  66.   SPACE = 57,
  67.  
  68.   -- Arrow Keys
  69.   UP = 200,
  70.   DOWN = 208,
  71.   RIGHT = 205,
  72.   LEFT = 203,
  73. }
  74.  
  75. local pressedCodes = {}
  76.  
  77. -------------------------------------------------------------------------
  78.  
  79. function keyboard.isKeyDown(code)
  80.   checkArg(1, code, "number")
  81.  
  82.   return pressedCodes[code]
  83. end
  84.  
  85. function keyboard.isControl(code)
  86.   return type(code) == "number" and (code < 32 or (code >= 127 and code <= 159))
  87. end
  88.  
  89. function keyboard.isAltDown()
  90.   return pressedCodes[keyboard.LEFT_ALT] or pressedCodes[keyboard.RIGHT_ALT]
  91. end
  92.  
  93. function keyboard.isControlDown()
  94.   return pressedCodes[keyboard.LEFT_CONTROL] or pressedCodes[keyboard.RIGHT_CONTROL]
  95. end
  96.  
  97. function keyboard.isShiftDown()
  98.   return pressedCodes[keyboard.LEFT_SHIFT] or pressedCodes[keyboard.RIGHT_SHIFT]
  99. end
  100.  
  101. function keyboard.isCommandDown()
  102.   return pressedCodes[keyboard.COMMAND_KEY]
  103. end
  104.  
  105. -------------------------------------------------------------------------------
  106.  
  107. require("grapes.Event").addHandler(function(e1, _, _, e4)
  108.   if e1 == "key_down" then
  109.     pressedCodes[e4] = true
  110.   elseif e1 == "key_up" then
  111.     pressedCodes[e4] = nil
  112.   end
  113. end)
  114.  
  115. -------------------------------------------------------------------------------
  116.  
  117. return keyboard
  118.  
Tags: ORMS
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement