Last active
October 20, 2024 19:33
-
-
Save c-l-nguyen/7e1bd3ef3c1e39087934e9f6fd23618d to your computer and use it in GitHub Desktop.
Hammerspoon virtual numpad for macOS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
k = hs.hotkey.modal.new('ctrl-shift', 'n') | |
function k:entered() hs.alert'Virtual Numpad' end | |
function k:exited() hs.alert'Exit Virtual Numpad' end | |
k:bind('ctrl-shift', 'n', function() k:exit() end) | |
hs.fnutils.each({ | |
{ key='j', padkey='pad1'}, | |
{ key='k', padkey='pad2'}, | |
{ key='l', padkey='pad3'}, | |
{ key='u', padkey='pad4'}, | |
{ key='i', padkey='pad5'}, | |
{ key='o', padkey='pad6'}, | |
{ key='7', padkey='pad7'}, | |
{ key='8', padkey='pad8'}, | |
{ key='9', padkey='pad9'}, | |
{ key='m', padkey='pad0'}, | |
{ key='/', padkey='pad+'}, | |
{ key=';', padkey='pad-'}, | |
{ key='p', padkey='pad*'}, | |
{ key='0', padkey='pad/'}, | |
}, function(vmap) | |
k:bind({}, vmap.key, | |
function() hs.eventtap.keyStroke({}, vmap.padkey, 20) end, | |
nil, | |
function() hs.eventtap.keyStroke({}, vmap.padkey, 20) end) | |
end | |
) |
Good job. Thanks for sharing.
Any idea why it might not be working in virtual machine apps?..
@andkirby not sure in that case. I suspect the keyboard shortcut conflicts with another one in the virtual machine but that's just a guess right now since I don't have access to a macOS VM at the moment.
I have experience with mouse movements and clicks. So built-in mechanics are not working in this VM. The mouse moves, but the VM does not respond to that action, e.g. some hover action should be triggered and clicks did not work.
And I ended up with using shell CLI cliclick
app and use it directly. Probably, HS has some common issue with emulating that does not in VMs.
Code
-- Function to simulate mouse click at specific coordinates using cliclick
local function clickAt(x, y)
hs.execute(string.format("/opt/homebrew/bin/cliclick c:%d,%d", tonumber(x), tonumber(y)))
end
-- Function to simulate mouse movement using cliclick
local function moveMouse(x, y)
hs.execute(string.format("/opt/homebrew/bin/cliclick m:%d,%d", tonumber(x), tonumber(y)))
hs.execute('sleep 0.25')
end
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Alright I think I got it figured out. Try this config. It will make ctrl-shift-n toggle virtual numpad. (Feel free to change that.)
Just install Hammerspoon, choose "File >Open Config", paste this in, save, then go back to Hammerspooon and choose "Reload Config”. On macOS Catalina, be sure to go into System Preferences > Security & Privacy > Privacy > Accessibility > Check "Hammerspoon.app"