-
Notifications
You must be signed in to change notification settings - Fork 574
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
eventtap for ctrl+shift+eject increments #2115
Comments
If you just want CONTROL+SHIFT+EJECT to sleep the display, you just need one myEventtap = hs.eventtap.new({ hs.eventtap.event.types.flagsChanged, hs.eventtap.event.types.NSSystemDefined }, function(event)
local flags = event:getFlags()
local systemKey = event:systemKey()
if flags.ctrl and flags.shift and systemKey.key == "EJECT" and systemKey.down then
print("test")
os.execute("pmset displaysleepnow")
end
end):start() |
Awesome thanks @latenitefilms I suspected this could be it, but I couldn't work out how to use only one hs.eventtap However this doesn't seem to work, "test" not printing in console and display not turning off. |
Ummm, it worked fine here on my MacBook Pro with an external keyboard. Try pasting it in the Hammerspoon Console, and see if it works there? Do you see any error messages? |
I have an Apple USB keyboard. I disabled my current init.lua and made a new one containing your code (I commented out os.execute for testing) Console when reloading config:
Still not working unfortunately - 'test' not printing. Console when pasting in your lua code:
No errors. |
Strange, well, it's working fine here. You can try adding in some debugging code: myEventtap = hs.eventtap.new({ hs.eventtap.event.types.flagsChanged, hs.eventtap.event.types.NSSystemDefined }, function(event)
local flags = event:getFlags()
local systemKey = event:systemKey()
if flags.ctrl and flags.shift and systemKey.key == "EJECT" then
print("CONTROL+SHIFT+EJECT Pressed!")
return true
else
print(string.format("flags: %s", hs.inspect(flags)))
print(string.format("systemKey: %s", hs.inspect(systemKey)))
end
end):start() |
Debug output. Held rightctrl+rightshift and then pressed eject and then quickly released all three keys. It seems "CONTROL+SHIFT+EJECT Pressed!" wasn't printed. I've also tried left ctrl and left shift.
|
Looks like, at least on your keyboard, when the EJECT key is pressed, it ignores the modifier flags in the event it generates (notice that I wonder if it's clearing them globally or just for the event itself... try this: myEventtap = hs.eventtap.new({ hs.eventtap.event.types.flagsChanged, hs.eventtap.event.types.NSSystemDefined }, function(event)
local flags = event:getFlags()
local systemKey = event:systemKey()
if flags.ctrl and flags.shift and systemKey.key == "EJECT" then
print("CONTROL+SHIFT+EJECT Pressed!")
return true
else
print(string.format("flags: %s", hs.inspect(flags)))
print(string.format("flags from instant check: %s", hs.inspect(hs.eventtap.checkKeyboardModifiers())))
print(string.format("systemKey: %s", hs.inspect(systemKey)))
end
end):start() I've added a line in the debugging output which uses |
Thanks @asmagill very much appreciated. Debug output
|
With Karabiner quit it works. God damn it Karabiner 😂 Debug output
|
Try this: myEventtap = hs.eventtap.new({ hs.eventtap.event.types.flagsChanged, hs.eventtap.event.types.NSSystemDefined }, function(event)
local flags = hs.eventtap.checkKeyboardModifiers()
local systemKey = event:systemKey()
if flags.ctrl and flags.shift and systemKey.key == "EJECT" then
print("CONTROL+SHIFT+EJECT Pressed!")
return true
end
end):start() |
Awesome this works This is when I am press ctrl+shift+eject all at the same time
This is when I press ctrl+shift and then press eject (what I have been used to after years of using macOS)
I'll take it! Thanks @latenitefilms @asmagill ! |
You could try: myEventtap = hs.eventtap.new({ hs.eventtap.event.types.flagsChanged, hs.eventtap.event.types.NSSystemDefined }, function(event)
local flags = hs.eventtap.checkKeyboardModifiers()
local systemKey = event:systemKey()
if flags.ctrl and flags.shift and systemKey.key == "EJECT" and systemKey.down and not systemKey.repeat then
print("CONTROL+SHIFT+EJECT Pressed!")
return true
end
end):start() |
I guess it's expecting something else for |
Ummm, try: myEventtap = hs.eventtap.new({ hs.eventtap.event.types.flagsChanged, hs.eventtap.event.types.NSSystemDefined }, function(event)
local flags = hs.eventtap.checkKeyboardModifiers()
local systemKey = event:systemKey()
if flags.ctrl and flags.shift and systemKey.key == "EJECT" and systemKey.down and not systemKey["repeat"] then
print("CONTROL+SHIFT+EJECT Pressed!")
return true
end
end):start() |
Perfect! Pressing ctrl+shift and then eject:
|
Woohoo! Got there in the end. Please close this issue if you're all good. Feel free to re-open later down the line if needed. Happy coding! |
Thanks a lot @latenitefilms I really appreciate it. Final script with sleep and shutdown added. myEventtap = hs.eventtap.new({ hs.eventtap.event.types.flagsChanged, hs.eventtap.event.types.NSSystemDefined }, function(event)
local flags = hs.eventtap.checkKeyboardModifiers()
local systemKey = event:systemKey()
if flags.ctrl and flags.shift and systemKey.key == "EJECT" and systemKey.down and not systemKey["repeat"] then
os.execute("pmset displaysleepnow")
return true
end
if flags.alt and flags.cmd and systemKey.key == "EJECT" and systemKey.down and not systemKey["repeat"] then
os.execute("pmset sleepnow")
return true
end
if flags.ctrl and flags.cmd and systemKey.key == "EJECT" and systemKey.down and not systemKey["repeat"] then
hs.osascript.applescript([[
tell app "System Events" to shut down
]])
return true
end
end):start() note: I have root access to /usr/bin/pmset in sudoers |
I am trying to make a module for mapping ctrl+shift+eject to sleep the display.
(Karabiner Elements ignores the eject key while launched, the display sleep shortcut works while Karabiner is closed.)
I've made the following with help from the following link. It works, however it seems to run the second eventtap incrementally i.e. once the first time, twice the second time etc.
#1220 (comment)
https://blog.theodo.com/2018/03/making-runtime-funtime-hammerspoon/ (last piece of code)
Console:
Any ideas? Thanks.
The text was updated successfully, but these errors were encountered: