Skip to content
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

Hotkey script results in garbled text on remote Windows server #3649

Closed
xinxianwu opened this issue Jun 22, 2024 · 6 comments
Closed

Hotkey script results in garbled text on remote Windows server #3649

xinxianwu opened this issue Jun 22, 2024 · 6 comments

Comments

@xinxianwu
Copy link

I wrote the following script. I expect that when I press Ctrl+Shift+W while remotely connected to a Windows server, it will input "Welcome1". However, I am getting a string of garbled text instead. I am not sure how to fix this.

hs.hotkey.bind({"ctrl", "shift"}, "w", function()
    hs.eventtap.keyStrokes("Welcome1")
end)

image

@Rhys-T
Copy link

Rhys-T commented Jul 15, 2024

Are you using Microsoft Remote Desktop to connect to the server? It looks like it's known to have issues with the key events generated by Hammerspoon: #1056, #2281. According to #2881 (comment), changing Remote Desktop's 'Keyboard Mode' setting from 'Scancode' to 'Unicode' can work around the problem. If I understand correctly, this basically makes Remote Desktop send which character is being typed, rather than which key.

hs.eventtap.keyStrokes seems to set the character for the key events it sends (using CGEventKeyboardSetUnicodeString), but not the keycode, which means that the keycode stays at the default of 0 (the A key). So it looks to Remote Desktop like you keep pressing A, but in a different keyboard layout each time, mapping that 'key' to each character of the string you're trying to type. In Scancode mode, MSRD is ignoring that mapping and letting the Windows server decide what character to insert based on the keycode1… which isn't set correctly. If you switch it to Unicode mode, it should use the string set by HS instead. (But it will also start using your local Mac's keyboard layout rather than the one selected on the Windows server, if that's different.)

You can also send each key individually using hs.eventtap.keyStroke or hs.eventtap.event.newKeyEventSequence, which should set the keycodes properly, but you'd need to figure out the correct combination of key + modifiers for each character yourself - Hammerspoon won't do it for you when using those functions.

Footnotes

  1. Normally that would be a like in the issues I mentioned above. My guess is that since you still have Control (and Shift) pressed from the hotkey at that point, it's ending up as a Control-A character - a.k.a. Start of Heading, or .

@xinxianwu
Copy link
Author

I logged into the Windows server through Royal TSX and tried changing key presses to Unicode, but the garbled characters issue still hasn't been resolved.

CleanShot 2024-07-17 at 12 52 51@2x

@cmsj
Copy link
Member

cmsj commented Aug 5, 2024

I'd be interested to know if hs.eventtap.keyStroke() works. Perhaps we need a variant of keyStrokes() which just sends key sequences (although then we have the interesting challenge of trying to transform a unicode input string into keycodes!)

@latenitefilms
Copy link
Contributor

We need to merge in @asmagill's hs.text - that's what I use in CommandPost for unicode handling.

Copy link

github-actions bot commented Sep 5, 2024

This issue needs more information. Please see our request above.

@github-actions github-actions bot added the stale label Sep 5, 2024
Copy link

github-actions bot commented Oct 5, 2024

No information was received, closing this Issue for now.

@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Oct 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants