Jump to content

Sending Keyboard Modifiers (Ctrl etc.)


Recommended Posts

A lot of Alfred workflows and commands have options when certain keys are pressed.  These keystrokes can be sent from Alfred Remote with a bit of Applescript.  e.g. to hold and release the Control key, add the following:

 

Alfred Preferences > Remote > System Commands (or wherever you want on your remote) > + > Run Script > language: /usr/bin/osascript

 
then paste in the following short Applescript:
tell application "System Events"
	control key down
end tell

Name this item "Hold Ctrl" but BEFORE YOU TEST IT add a second command following the same procedure:

tell application "System Events"		
	control key up
end tell

Name this 2nd item "Release Ctrl".

 

Now you can test it and make sure it works.  If you test the first script before adding the second, you will end up with a practically unusable computer b/c the Control key will be down and you will have no way to release it.  I found this out the hard way and had to reboot.

 

IMPORTANT - this does not work with the Option key for reasons that are unclear.  You can create an Applescript to hold the Option key down, and it will work, but the corresponding script to release does not work, and you'll be forced to reboot to get your keyboard working normally again.

Edited by dfay
Link to comment

I tried to do this for system volume. But it won't work. What is wrong with "key code 111"?

Hey! Another total beginner at scripts I suppose? I finally got THIS to work: 

 

 

tell application "System Events" to keystroke "k"

 

 

So that is the menu button for PLEX: I'm of to try try up-arrow ;)

Link to comment

Hey! Another total beginner at scripts I suppose?

;)

I finally got THIS to work: 

 

 

tell application "System Events" to keystroke "k"

 

 

So that is the menu button for PLEX: I'm of to try try up-arrow ;)

So "keystroke k" makes the system louder? Thanks a lot! :D

Edited by heldausberlin
Link to comment

Okey, I might be out of league here and I probably didn't understand what you meant.

 

I googled a little bit and for volume up I got this to work:

 

tell application "System Events" to key code "103"

 

 

Remember to set the script in Alfred to osascript (whatever that is)

 

 

Anyhow as a total beginner it was really fun when I just got "downarrow to work" with this script:

 

tell application "System Events" to key code "125"

 

 

What a feeling. ;) I'm off building the rest of the remote now with The Google.

;)





So "keystroke k" makes the system louder? Thanks a lot! :D

Link to comment

I need help with a script that is a little bit to advanced for me...

 

I need to send the command "CMD+SHIFT+LeftArrow" and hold it down for 25ms but Not longer. 

 

This is what I use to control the volume on my GLM audio system. (it has a 20ms minimum for safety)

 

I tried to write it myself but it was to complicated for me? Could anyone help me out? ;/

 

Thanks a lot!

/M

Link to comment

I need help with a script that is a little bit to advanced for me...

 

I need to send the command "CMD+SHIFT+LeftArrow" and hold it down for 25ms but Not longer. 

 

This is what I use to control the volume on my GLM audio system. (it has a 20ms minimum for safety)

 

I tried to write it myself but it was to complicated for me? Could anyone help me out? ;/

 

Thanks a lot!

/M

 

What are you actually trying to do? What program are you trying to interact with?

 

To do exactly what you want, then you do something like:

tell application "System Events"
	--- this is where you put the key down commands
	delay 0.025
	--- this is where you put the key up commands
end tell

However --- let me rephrase that: HOWEVER, AppleScript's delay command isn't very accurate as it was designed for seconds and not milliseconds, so you're likely not going to get the results that you want.

Link to comment

What are you actually trying to do? What program are you trying to interact with?

 

To do exactly what you want, then you do something like:

tell application "System Events"
	--- this is where you put the key down commands
	delay 0.025
	--- this is where you put the key up commands
end tell

However --- let me rephrase that: HOWEVER, AppleScript's delay command isn't very accurate as it was designed for seconds and not milliseconds, so you're likely not going to get the results that you want.

Thank you Shawn!

 

I have an application called GLM that changes the volume for my Genelec active digital speakers. It is a completely digital system (No D/A) with the sound running over AES protocol via XLR cables and the volume commands is send to each speaker via CAT5. This means that I can no longer change the volume for my mac using the OS volume control (it is greyed out). I can only change the volume of the sound played on my mac with this hotkey (CMD+SHIFT+Left/rightArrow that tells the GLM software to send a signal via CAT5 to the speakers.

 

The only problem is that if the keyboard shortcut is activated for to long the sound from the speakers will eventually reach 100db and if it is pressed for to short of a time the volume will change hardly at all. It works great with the keyboard (even over VNC) but som remotes can't send the shortcut enough times per second or sometimes it cannot keep sending it for the time it needs to send it. It works pretty good with HippoRemote but that app haven't been updated for over two years and is slowly falling apart when OS X is updated.

 

I can set the volume cmd timer in GLM to 10-50 ms but I prefer it at 50 because it works best when using the keyboard. If I put it to 10ms the volume gets really loud really fast which can enjoy the neighbors if a friend is messing around with my sound not knowing how it works

 

So if I understand correctly the command we could try would look like this? (replacing CMD etc. with the real codes for those keys)

 

tell application "System Events"

 to key code "CMD+SHIFT+125"

    delay 0.025

  to key code "CMD+SHIFT+125"

end tell

 

Then I would have to experiment (with no sound on, hehe) to find the sweet spot for milliseconds. 

 

A feature request to Alfred Remote would be to be able to send keystrokes that keeps being send while you have your tump pressed on the button on Alfred remote.

 

So that's it... I know it sounds like a bit of a hazel but it the sound is good :).

Link to comment

If you want to make this reliable, then you should not base this on time (again, because AppleScript does not understand milliseconds). More importantly, you should see if there is a way to interact with GLM directly rather than using Hotkeys that are setup.

 

So, does GLM support AppleScript? If it provides AppleScript commands to set the volume, then you should definitely use those instead of trying to get the timing right with the delays because your results will be more accurate.

 

I'm not sure that it does. The easiest way to tell would be to open Script Editor and then click File ->Open Dictionary. Then, if there is an entry for the GLM program, you'll see what commands are available. If that isn't there, then you can always try UI scripting (see this Stackoverflow answer: https://stackoverflow.com/questions/23847324/applescript-set-focus-to-a-slider-before-sending-key-presses-up-down).

 

Otherwise, you can try the timing one, which should be the code above... mostly. But it doesn't look like AppleScript recognizes key down instructions for non-modifier keys (at least it doesn't seem to very well).

 

So, the code might have to be more like

tell application "System Events"
	key down shift
	key down command
	repeat 125 times
		keystroke (key code 123)
	end repeat
	key up shift
	key up command
end tell

So that code will press the left arrow 125 times while holding shift and command down.

Link to comment

Okay, guys. I guess I'm a noob with AppleScript.

I tried to insert your codes into a Remote Workflow. Once as AppleScript and once as Script with "osascript". Nothing happens when I touch the button on the app. What goes wrong? Thanks for helping me.

 

Are you just trying to do the key strokes or are you putting them inside a code block that talks to "System Events"?

 

Look here for more about "System Events": https://en.wikibooks.org/wiki/AppleScript_Programming/System_Events.

Link to comment

Thanks. I think I've mixed too much. :( If I want to use the "set volume …", is it to tell "System events" as well?

 

No. You shouldn't need to do so.

 

Okay. Here is a script that I just wrote for you.

on increase_volume(step_size_)
	set volume_ to output volume of (get volume settings)
	if volume_ > (100 - step_size_) then
		set volume output volume 100
	else
		set volume output volume (volume_ + step_size_)
	end if
	if output muted of (get volume settings) is true then
		set volume without output muted
	end if
end increase_volume

on decrease_volume(step_size_)
	set volume_ to output volume of (get volume settings)
	if 0 > (volume_ - step_size_) then
		set volume output volume 0
		set volume with output muted
	else
		set volume output volume (volume_ - step_size_)
	end if
end decrease_volume

on mute_volume()
	set volume with output muted
end mute_volume

on unmute_volume()
	set volume without output muted
end unmute_volume 

That defines four functions (handlers): increase_volume, decrease_volume, mute_volume, and unmute_volume.

 

Now, this is not complete because for each action, you need to declare what one you're doing. (I don't know you're writing this as a workflow or as simple run applescripts....). If you want to create a workflow with those actions, then use this script, and then use an argument to figure out which function to call. (Note: step_size_ is a variable which is how much you want to increase the volume. Min volume is 0, max volume is 100. If your step_size_ is 7, then it will increase by 7 each time. This can use integers only).

 

If you want to just have a "run script" for each, then cannibalize the functions. In other words, create a "Volume Up" remote button that is a "Run Applescript". In that box, put in this:

on alfred_script()
	set step_size_ to 7
  	set volume_ to output volume of (get volume settings)

	if volume_ > (100 - step_size_) then
		set volume output volume 100
	else
		set volume output volume (volume_ + step_size_)
	end if
	if output muted of (get volume settings) is true then
		set volume without output muted
	end if
end alfred_script

(And adjust the step size to whatever you want).

 

Do the same thing for Decrease Volume, etc...

Link to comment

O thanks!

Now I did it like this (I didn't know it needs just the code without any "alfred_script" or sth else).

 

for mute Button:

set volume with output muted

for plus Button:

set volume output volume (output volume of (get volume settings) + 5) --100%

for minus Button:

set volume output volume (output volume of (get volume settings) - 5) --100%
Link to comment

If you want to make this reliable, then you should not base this on time (again, because AppleScript does not understand milliseconds). More importantly, you should see if there is a way to interact with GLM directly rather than using Hotkeys that are setup.

 

So, does GLM support AppleScript? If it provides AppleScript commands to set the volume, then you should definitely use those instead of trying to get the timing right with the delays because your results will be more accurate.

 

I'm not sure that it does. The easiest way to tell would be to open Script Editor and then click File ->Open Dictionary. Then, if there is an entry for the GLM program, you'll see what commands are available. If that isn't there, then you can always try UI scripting (see this Stackoverflow answer: https://stackoverflow.com/questions/23847324/applescript-set-focus-to-a-slider-before-sending-key-presses-up-down).

 

Otherwise, you can try the timing one, which should be the code above... mostly. But it doesn't look like AppleScript recognizes key down instructions for non-modifier keys (at least it doesn't seem to very well).

 

So, the code might have to be more like

tell application "System Events"
	key down shift
	key down command
	repeat 125 times
		keystroke (key code 123)
	end repeat
	key up shift
	key up command
end tell

So that code will press the left arrow 125 times while holding shift and command down.

Wow, thank you Shawn!

 

I'm going to test this first time tomorrow on my day off :)

Link to comment

Now I know that I can send (hold) certain keystroke from Remote; however, it the purpose is only for executing optional actions within a workflow, there should be a better way.

 

For example, can we have an option to "Hold modifier keys" while adding a command or workflow to the remote panel ? Alfred would then know to hold these keys while the remote action is triggered.

 

Please consider this as a feature request.

Link to comment

I guess great minds think the same, I wrote a remote workflow and submitted to packal (waiting to get approved) that will control the volume on your mac

 

http://www.packal.org/workflow/remote-volume-control

Great! If you like, you could change the code for the mute button with:

if output muted of (get volume settings) is true then
set volume without output muted
else
set volume with output muted
end if

Then you don't need two buttons for Mute/Unmute. You can tap the same button for both functions. Also you can change the background color in the same green tone.

Edited by heldausberlin
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...