Trying to change keymap and layer to default base when power on computer

I’m trying to make it so when I power on my computer, the keyboard automatically swaps from whatever keymap and layer it’s on over to my default map and base layer. Using the $onInit smart macro, I was unable to get it to do anything. Anyone have a solution?

So far I’ve tried:

delayUntil 5000
ifNotKeymap QWR switchKeymap QWR
ifNotLayer base unToggleLayer

and

delayUntil 5000
ifNotKeymap QWR switchKeymap QWR
ifNotLayer base toggleLayer base

Hi!

You are on the right track, except for a few details.

The $onInit macro is executed only when UHK is booted up (i.e., connected to power), and on config saves. What you quite clearly mean is that you want UHK to switch layers/keymaps when the computer is resumed from a “suspend to RAM” mode. When computer is asleep, UHK is still powered, although the computer communicates the suspension state over USB, so UHK knows to turn its LEDs off, which is why it looks turned off, but in reality it is just in a sleep state. So when the computer wakes up, UHK just turns its LEDs on again, and there is no $onInit call.

Although the firmware knows about the computer states, we provide no way to act on them in smart macros.

As to solution, it is possible to send keymap switch commands as well as macro commands over USB, so what I suggest is for you to use these and hook a script onto the computer’s wakeup.

As to how to trigger UHK actions from your computer, there is a brief guide at the end of firmware/doc-dev/user-guide.md at master · UltimateHackingKeyboard/firmware · GitHub .

The guide to doing this has me confused. I’m using the Linux Agent appimage, not a build of Agent, so no clue as to where this would be pointing me to or how to do this. Does this mean I need to build from source code or can I work with what I have? Which then leads to how to setup the on-power/wake script. I’m still fairly new to Linux so setting up start-up/wake scripts is not in my knowledge base yet.
Would the script say something like “keyboard execute this macro” which said macro is saved on the keyboard? Or is it that the script is doing the commands itself?

I think I’ve found that Linux Mint will let me run a command on startup and set it within the Startup Applications menu. Just need to figure out how to make the script talk to the keyboard now and what the script actually says to do.

I see.

  • One way is to build from source code.
  • Another way is to unpack the appimage and search for the relevant script there. I don’t know whether it is packed with releases, but I would guess that it is.

But given that you are on linux, using the /dev/hidraw* device will probably be the most convenient one:

Create a script with the following content. Say /usr/local/bin/uhkcmd:

#!/bin/bash
hidraw=`grep 'UHK 60' /sys/class/hidraw/hidraw*/device/uevent | LC_ALL=C sort -rh | head -n 1 | grep -o 'hidraw[0-9][0-9]*'`
echo -e "\x14$*" > "/dev/$hidraw"

Furthermore you need to make it executable: e.g., with chmod a+x /usr/local/bin/uhkcmd in the shell.

If you placed it at some other place, you also need to make that place be part of your PATH environment variable (which can be done by placing something like export PATH="/your/path/:$PATH" into your ~/.profile) or execute it using an absolute path.

Test the script with uhkcmd "setLedTxt 5000 Hey" in your shell. (Your segment display should display “hey” for 5 seconds.)

Now you can execute the macro commands as this from your shell:

uhkcmd "ifNotKeymap QWR switchKeymap QWR"
sleep 0.1
uhkcmd "ifNotLayer base unToggleLayer"

As to hooking things in linux, that largely depends on your window manager. Personally, I am suspending my computer using a custom script that takes care of all related automation (controlling some smart light bulbs, adjusting screensaver, locking the screen, setting up an alarm using rtcwake, …).

As to global ways, here are some chatgpt recommendations: https://chatgpt.com/c/21b34e53-0eba-474a-b22c-75124b07a3e3, although I would advise against using them, as the systemd approach is very temperamental (in the sense that I remember trying to use it for some things in the past and wasn’t very successful).

Another, much more hackish way is to write a script, make your window manager to execute it on startup (or more precisely login), that will mark current time every few seconds, and if there is larger gap between the last marked time than those few seconds, then you know that your computer was sleeping.


I think I’ve found that Linux Mint will let me run a command on startup and set it within the Startup Applications menu.

My guess is that the result will be the same as with $onInit: that it will execute only on fresh boot, but not on waking up from sleep.

Your guess appears to be correct about this. Would be nice if the keyboard could execute its own commands when it sees the computer start or wake up. Perhaps a future feature?

This link gives me nothing except a redirect to chatgpt start page. Considering your recommendation to not do that, probably best it didn’t give me the results then.

The test went fine and I can execute the commands via terminal/shell. I’ll do some digging as to how this can execute on startup or wake from suspend. Unfortunately google, rather than what I need, is filled with tons of “how do I wake my computer with a keyboard” no matter how I change the search wording. Thank you for the help.

My apologies.

See

Maybe. Feel free to open a github ticket about it, but to be honest I think this usecase is a bit too niche for us to support it.

Well I’ve done what I can but certainly a headache cause of the setting up linux startup/wake script/service. At this point, I don’t know if this is worth saving a few keystrokes, so may come back to it later when I have a better handling of the Linux environment. Thank you anyway, I learned a few things out of this conversation.

Doesn’t have to be implemented if truly too niche. My thoughts to it was that if it can already see the computer state to turn off/on its LEDs (which to me means commands are being executed behind the scenes), then why not be able to execute macro commands based on said state too. No biggie though. Again, this was just to save a few keystrokes cause I would have liked it to do that, but I can live without that if the linux system script is being too obnoxious.

Well, technicaly it is indeed not a hard thing to do.

The reason is more about us trying to keep the API surface reasonably small in order to make it easier to grasp. In other words not to clutter the grammar and docs and source code with large amounts of unnecesary commands.

Something like $onWake (would trigger whenever the LEDs are turned on again after they have gone to sleep)?

Yes.

1 Like