Using modifiers with RDP Windows session

I am trying to run AppleScript to send key codes to an RDP session (Windows Guest). Unfortunately none of modifiers seems to work.

Example, when I send Shift + v using key code 9 using {shift down}, RDP window receives lowercase v. I’ve tried various combinations of the key codes and modifiers and have only got the key codes (of letters and numbers) to work.
While it works perfectly fine on Mac (host).

Sample code I am using:

tell application "System Events"
    tell process (path to frontmost application as string)
        key code 9 using {shift down}
    end tell
end tell

Is this not supported or am I doing something wrong?

AppleScript: 2.7
Browser: Safari 537.36
Operating System: macOS 10.14

  1. You should first hide Script Editor (or Script Debugger).
  2. Getting the name of frontmost application process is wrong in your script too. For example:

tell application "System Events"
	-- Hiding Script Debugger.
	set visible of process "Script Debugger" to false
	-- Hiding Script Editor.
	-- set visible of process "Script Editor" to false
	set theAppName to name of item 1 of (processes whose frontmost is true)
	tell application process theAppName to key code 9 using shift down
end tell

-- Showing the result
tell application "Script Debugger" to activate -- or, tell application "Script Editor" to activate
theAppName

GUI scripting is not necessary to hide an application

tell application "System Events"
	set visible of process "Script Debugger" to false
end tell

Thanks. I made changes in my script. Universal way to Script Editor and Script Debugger using:


tell application "System Events"
       -- Get user's script editor name
	set scriptEditorName to name of item 1 of (processes whose frontmost is true)
	-- Hiding script editor
	set visible of process scriptEditorName to false
	-- Get name of app, which was frontmost before the script editor
	set appName to name of item 1 of (processes whose frontmost is true)
	-- Press shortcut
	tell process appName to key code 9 using shift down
end tell

-- Show the result
tell application scriptEditorName to activate
appName

Thanks!

Although my problem remains. It wont send or print (on notepad) uppercase V but only lowercase v.

I have never used RDP, so I can not understand the subtleties. In your script, I fixed only the obvious problem. In addition, you provide little information. If you provided at least a screenshot of what you are trying to do, there is probably someone who understands RDP.

Or, at least, what app name does my script return when run on your computer? Or, what does this shortcut you tried to send on this app? Is on remote computer enabled GUI keystrokes and key codes?

Probably, need send keystroke not to app but to its window, or to some element of its window.

And no need uppercase “V” to keystroke, it uses “v” syntax

Thanks again. At this point I am only trying to send some text with an uppercase letter (ex: Victory) to an open notepad in RDP (windows). I see that when I use keystroke or key code (individually sending each key with using shift down on v as shown in original post), it only prints victory and not Victory. It is not limited to this combination, any modifier I try, it is not being sent through (or received through) but only letters are printed (without modifiers).

With your script I get the app name as: Microsoft Remote Desktop
You mention checking enabling GUI keystrokes, where and how would I do that?

As I see, here no need modifiers. Replace this

tell application process theAppName to key code 9 using shift down

with this

tell application process theAppName to keystroke "Victoria"

I tried it again with keystrokes… and it only prints victoria and not Victoria.

I don’t believe :lol: And this?

tell application "System Events" to keystroke "Victoria"

or this?


tell application "System Events" to tell process appName to tell window 1 to keystroke "Victoria"

I have Microsoft Remote Desktop on my machine. But how can I connect to some Notepad for free to try myself?

Both still prints just victoria. No uppercase.

I was able keystroke in the Notepad word “Victoria” successfully (not on RDP, but on my Mac) only with this code:

tell application "System Events"
	-- Get user's script editor name
	set scriptEditorName to name of item 1 of (processes whose frontmost is true)
	-- Hiding script editor
	set visible of process scriptEditorName to false
	-- Get name of app, which was frontmost before the script editor
	set appName to name of item 1 of (processes whose frontmost is true)
	-- Press shortcut
	tell process appName -- "wineloader" without RDP
		set value of attribute "AXEnhancedUserInterface" to true
		keystroke "Victoria"
		delay 1
	end tell
end tell

-- Show the result
tell application scriptEditorName to activate
appName

NOTE: Notepad on my machine is in one wine bottle of Crossover.app. It seems, need delay after the keystroke :lol: Now I go to work.

Okay it worked. I updated my RDP client to latest and just works. Both using keystroke and key code I was able to pass modifiers. I am not sure what was up with the previous version of RDP I was using.

Thank you KniazidisR for helping me here.

I have a similar problem. I can’t figure out how to send the alt modifier to RDP. Here’s the script I have but it doesn’t work because alt is not supported:


events = Application("System Events")
events.keystroke("m", {
    using:['control down', 'alt down']
});

Using option down instead seems like it would work, but it doesn’t. Is there a way to send modifiers using their key codes instead? Something tells me this is the solution.

Not sure if it will help in your situation but the modifier keys do have codes.

[format]55 - command (either side)
57 - left shift*
58 - left option
59 - left control
60 - right shift
61 - right option
[/format]
I see 57 for both the left shift and the caps lock key but no 56, so it’s possible that I have that code wrong and that the other is 56.

Yeah, I guess there are two questions associated with this:

  1. How do you send modifier keys by their key codes in a way that they are ‘down’ in concert with another key?
  2. Can I send a non-mac key (alt)?

Basically, how KniazidisR explained it. I think you have been mixing ‘key code’ with ‘keystroke’.

Key code is part of System Events. You can view the syntax in the System Events dictionary (along with keystroke) but basically there are three styles. The code by itself… in this case the letter ‘v’. To get a single modifier key, append with ‘using command down’ (or shift, option, control). Finally, to get multiple modifier keys, place them inside curly braces.

Do not use quotes of any kind as you’re not sending text (as with ‘keystroke’).

tell application "System Events"
	
	key code 9
	key code 9 using command down
	key code 9 using {command down, option down}

end tell

The ‘alt’ key isn’t a non-mac key.

FWIW, while I don’t do much remotely, some apps have settings to determine how the software/remote machine should interpret various commands. Perhaps RDP does too.

OK. Yeah, this is kind of what I expected. There is no easy way to call a modifier by their key code (at least with what comes with Script Editor out of the box) and you can’t send the alt keydown modifier on a mac. There is a way to configure RDP but the problem is I’m two RDP sessions deep and I don’t have control over the settings for the 2nd RDP session (hence why I’m just trying to send the keys the 2nd RDP expects; seemed like the simplest approach to me, but alas, I realize it’s not sigh).

I’m not sure I understand you.

‘key code’ is not ‘keystroke’

It is very straightforward to send it. If you want to send ‘alt-control-m’ then this command will do so. How much easier can it be?

tell application "System Events" to key code 46 using {option down, control down}