I switched to Ubuntu recently, and I miss autohotkey. I'm using autokey to recreate my hotkey environment. I've remapped CapsLock
to F13
using Xmodmap.
What I need to do:
When F13
is tapped, return <Esc>
.
When F13
is used with a key, trigger hotkey.
When F13
is held for over 1 second and released with no hotkey, return nothing.
Autokey uses a Python environment. Here's my plan:
F13 is pressed
Start a timer
Start a thread listening for <CapsLock up> and if true,
if timer is less than 1 second && no hotkey was pressed
exit script after returning <Esc>
exit script
Start a thread that loops forever
Listen for hotkey
Play hotkey's function
The script ends when CapsLock
is released.
Example: I press CapsLock
then j
and output is Down arrow
.
My question before I start coding this is, do I REALLY need to use multiple threads (concurrency) for this? Is this the best way to do it? I feel like there is a much simpler way, and I've also never coded with concurrency.
Edit: I'm open to any method to pull this off, even if it's not with autokey or python.
You definitely don't need to use threads in this case. You can just do something like this:
F13 is pressed
Start timer
While True:
Listen for hotkey and capslock up
if capslock up:
if timer < 1:
return <Esc> and exit
else: just exit
elif hotkey:
Execute hotkey function and exit
The only two ways we exit are if capslock is released or if the hotkey's pressed, and only one of those two will ever be the event we need to worry about, so we can just listen for both in the same thread.