Waiting for any keypress to reset LED text

I’m trying to set up my UHK to display notification-like text from my computer and then clear it once the user presses any key.

I have figured out how to set LED text indefinitely with setLedTxt 0 BEL (with “BEL” being text that denotes a notification).

What I have not figured out how to do from the reference manual and am not sure are possible:

  1. Sleeping the macro/pausing execution to wait for any keypress at all–I have tried experimenting with ifShortcut, but haven’t gotten anything to work so far.
  2. Resetting the LED text to its default value–it seems like making another call to setLedTxt, but this time with a definite timeout does the trick. But is there a more “proper” way I should be doing this?

Thanks in advance for any pointers. I’m new to UHK macros, so still getting my sea legs.

  1. Try using a loop that waits for ifInterrupted. Somethink like ifNotInterrupted goTo @0. It is ugly way, but I can’t think of anything more proper.
  2. No. Your way is the one to go with.
1 Like

ifNotInterrupted goTo @0 works great! Thanks so much. Agent told me to switch from @0 to $currentAddress. So now I’ve got this nicely-working macro:

setLedTxt 0 "BEL"
ifNotInterrupted goTo $currentAddress
setLedTxt 1 "BEL"

One last thing: is there anything I should do to manage or prevent multiple concurrent invocations of this macro when invoking with call or exec via serial commands? My thinking is that if this macro gets called a lot without receiving any keypresses/without completing, it would result in duplicate invocations running at the same time. Maybe that’s not a problem and I’m overthinking it. Especially considering that I won’t be spamming this macro to begin with.

Potentially, I could deduplicate calls to this macro on the sending side, but if there’s a proper way to manage this on the UHK side of things, I would vastly prefer that.

You could set a global variable as a semaphore to signal that the macro is already running, and prevent entering the loop a second time.

2 Likes

Ah, indeed!

As max noted, there is only the manual way via variables.

That works great! Thank you both for the excellent (and speedy!) help!

For anyone who is interested, here’s what I ended up with.

“bell” macro:

if ($bellWaiting) {
    exit
}
setVar bellWaiting true
setLedTxt 0 "BEL"
ifNotInterrupted goTo $currentAddress
setLedTxt 1 "BEL"
setVar bellWaiting false

And I have this bell function defined in my shell. Depending on the occasion, I might call it myself (e.g. commandThatMightFail || bell, longRunningCommand; bell), or my shell might, according to some extra configuration I have set up that can optionally call bell after any function that takes a long time to run.

# bell.fish
function bell
	# uses the terminal's built-in functionality to "ring the bell."
	# my terminal (Kitty) makes an audible tone
	# and shows a visual indicator
	tput bel
	# "bell" is the name of the UHK macro to call
	type -q uhkcmd && uhkcmd "exec bell" 2> /dev/null
end

Thanks to this UHK macro, I get not only visual and auditory feedback from my terminal, but also visual feedback from my keyboard! That has the advantage of working in the TTY as well.

2 Likes

That’s pretty cool. Thanks for sharing it!