Advertisement
billysback

hotkeys

Jun 2nd, 2013
266
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.77 KB | None | 0 0
  1. --hotkeys
  2. hk_hotkeys = {}
  3. hk_toggle = true
  4.  
  5. function hk_register(key, command, env, ...)
  6.     if hk_hotkeys[key] == nil then
  7.         local args = { ... }
  8.         hk_hotkeys[key] = {command, env, args}
  9.         return true
  10.     else
  11.         return false, hk_hotkeys[key]
  12.     end
  13. end
  14.  
  15. function hk_unregister(key)
  16.     if hk_hotkeys[key] == nil then
  17.         return false
  18.     else
  19.         local command = hk_hotkeys[key]
  20.         hk_hotkeys[key] = nil
  21.         return true, command
  22.     end
  23. end
  24.  
  25. function hk_hotkey(key)
  26.     local command = hk_hotkeys[key]
  27.     if command == nil then
  28.         return false
  29.     else
  30.         if toggle then
  31.             local com = command[1]
  32.             local env = command[2]
  33.             if env == nil then env = {} end
  34.             os.run(env, com, unpack(command[3]))
  35.             return true
  36.         else
  37.             return false
  38.         end
  39.     end
  40. end
  41.  
  42. function hk_save()
  43.     --SAVE HOTKEYS
  44. end
  45.  
  46. function hk_load()
  47.     --LOAD HOTKEYS
  48. end
  49.  
  50. local old_pullEventRaw = os.pullEventRaw
  51. function new_pullEventRaw( _sFilter )
  52.     local event, p1, p2, p3, p4, p5 = os.coroutine.yield(_sFilter)
  53.     if event == "key" then
  54.         hk_hotkey(p1)
  55.     end
  56.     return event,p1,p2,p3,p4,p5
  57. end
  58.  
  59. function hk_off()
  60.     os.pullEventRaw = old_pullEventRaw
  61. end
  62.  
  63. function hk_on()
  64.     os.pullEventRaw = new_pullEventRaw
  65. end
  66.  
  67. local comArgs = {...}
  68. local command = comArgs[1]
  69. if command == "register" or command == "r" then
  70.     if #comArgs > 2 then
  71.         local key = comArgs[2]
  72.         local com = comArgs[3]
  73.         local suc, result = hk_register(key, com)
  74.         if suc then
  75.             print("Registered key '"..key.."' with command '"..com.."'")
  76.         else
  77.             print("Key '"..key.."' is already registered with command '"..result.."'")
  78.         end
  79.     else
  80.         print("Not enough arguments")
  81.     end
  82. elseif command == "unregister" or command == "u" then
  83.     if #comArgs > 1 then
  84.         local key = comArgs[2]
  85.         local suc, result = hk_unregister(key)
  86.         if suc then
  87.             print("Unregistered key '"..key.."' from command '"..result.."'")
  88.         else
  89.             print("Key '"..key.."' isn't registered!")
  90.         end
  91.     else
  92.         print("Not enough arguments")
  93.     end
  94. elseif command == "hotkey" or command == "h" then
  95.     if #comArgs > 1 then
  96.         local key = comArgs[2]
  97.         local suc = hk_register(key)
  98.         if suc then
  99.             print("Ran hotkey '"..key.."'")
  100.         else
  101.             print("Not hotkey exists for key '"..key.."' or hotkeys are toggled off!")
  102.         end
  103.     else
  104.         print("Not enough arguments")
  105.     end
  106. elseif command == "toggle" or command == "t" then
  107.     if hk_toggle == true then
  108.         hk_toggle = false
  109.         print("HotKeys toggled OFF")
  110.     else
  111.         hk_toggle = true
  112.         print("HotKeys toggled ON")
  113.     end
  114. elseif command == "off" then
  115.     hk_off()
  116. elseif command == "on" then
  117.     hk_on()
  118. elseif command == "help" or command == "h" then
  119.     if comArgs[2] == nil then
  120.         print("HotKey Commands;")
  121.         print("  toggle | t = toggle hotkeys on/off")
  122.         print("  register | r = register a new hotkey")
  123.         print("  unregister | u = unregister a hotkey")
  124.         print("  hotkey | h = run a hotkey")
  125.         print("  off = turn hotkeys off (safe mode)")
  126.         print("  on = turn hotkeys on")
  127.     else
  128.         local ch = comArgs[2]
  129.         if command == "register" or command == "r" then
  130.             print("HotKey Commands;")
  131.             print("  Help: hotkeys register KEY COMMAND")
  132.         elseif command == "unregister" or command == "u" then
  133.             print("HotKey Commands;")
  134.             print("  Help: hotkeys unregister KEY")
  135.         elseif command == "hotkey" or command == "h" then
  136.             print("HotKey Commands;")
  137.             print("  Help: hotkeys hotkey KEY")
  138.         elseif command == "toggle" or command == "t" then
  139.             print("HotKey Commands;")
  140.             print("  Help: toggles hotkeys on/off (basic)")
  141.         elseif command == "on" then
  142.             print("HotKey Commands;")
  143.             print("  Help: turns hotkeys on (safe mode off)")
  144.         elseif command == "off" then
  145.             print("HotKey Commands;")
  146.             print("  Help: turns hotkeys off (safe mode on)")
  147.         else
  148.             print("Help: Invalid command")
  149.         end
  150.     end
  151. else
  152.     print("Invalid Command, try 'hotkeys help'")
  153. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement