R3C0NF16UR3D

keyboarddriver.lua

Jul 31st, 2023
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.27 KB | None | 0 0
  1. local component = require("component")
  2. local gpu = component.gpu
  3. local event = require("event")
  4. local modem = component.modem
  5. local wm = require("windowmanager/libs/wm")
  6. local gui = require("windowmanager/libs/guiElements")
  7. local sh = require("sh")
  8. local fs = require("filesystem")
  9.  
  10. local ScreenWidth, ScreenHeight = gpu.getResolution()
  11.  
  12. local function identifyProgram(path)
  13. local file = io.open(path, "r")
  14. if file then
  15. l = file:read("*l")
  16. io.close(file)
  17. if l == "-- GUI --" then
  18. return true
  19. end
  20. end
  21. return false
  22. end
  23.  
  24. local keyboarddriver = {}
  25.  
  26. local version = "1.0"
  27.  
  28. local window
  29.  
  30. local function commandInputCallback(self, win)
  31. local command = gui.getElementText(self)
  32. gui.setElementText(self, "")
  33. wm.closeWindow(win)
  34. if command ~= "" then
  35. if fs.exists(command) then
  36. if identifyProgram(command) == true then
  37. wm.startProgram(command, 0)
  38. end
  39. end
  40. end
  41.  
  42. end
  43.  
  44.  
  45. local function runCommand()
  46. local windowWidth = 50
  47. local windowHeight = 4
  48.  
  49. window = wm.newWindow(math.floor(ScreenWidth/2) - math.floor(windowWidth/2), 2, windowWidth, windowHeight, "Run command")
  50. wm.setWindowSticky(window, true)
  51.  
  52. commandInput = gui.newInput(1, 2, windowWidth - 2, "", commandInputCallback)
  53. wm.addElement(window, commandInput)
  54.  
  55. wm.raiseWindow(window)
  56. end
  57.  
  58. function key_downCallback(_, _, ch, code)
  59. local alt = keyboard.isAltDown()
  60. local ctrl = keyboard.isControlDown()
  61. local shift = keyboard.isShiftDown()
  62. -- gpu.set(1,2, " ")
  63. -- gpu.set(1,2, tostring(ch) .. " " .. tostring(code) .. " " .. tostring(ctrl))
  64. if code == 3 and ctrl then -- ctrl-2
  65. runCommand()
  66. end
  67. end
  68.  
  69. function key_upCallback(_, _, ch, code)
  70. local alt = keyboard.isAltDown()
  71. local ctrl = keyboard.isControlDown()
  72. local shift = keyboard.isShiftDown()
  73. end
  74.  
  75.  
  76. function keyboarddriver.start()
  77. wm.registerDriver("keyboard", "A driver that handles background keyboard events", "S.Kempa", version)
  78. event.listen("key_down", key_downCallback)
  79. event.listen("key_up", key_downCallback)
  80. end
  81.  
  82. function keyboarddriver.stop()
  83. event.ignore("key_down", key_downCallback)
  84. event.ignore("key_up", key_downCallback)
  85. end
  86.  
  87. return keyboarddriver
Advertisement
Add Comment
Please, Sign In to add comment