Monday, October 26, 2020

#1 2020-10-09 11:33:07 pm

Sepa
Member
Registered: 2020-10-09
Posts: 5

Keyboard modifier key toggle on/off without keyboard input

Is it possible to toggle a keyboard modifer key or alpha numeric key via Applescript?
For example can Applescript turn the caps lock key or the option key on and off?

Model: Mac Book Pro 2015
AppleScript: 2.11
Browser: Safari 537.36
Operating System: macOS 10.14


Filed under: Modifier key toggle

Offline

 

#2 2020-10-11 10:38:51 am

KniazidisR
Member
Registered: 2019-03-03
Posts: 1352

Re: Keyboard modifier key toggle on/off without keyboard input

Sepa wrote:

Is it possible to toggle a keyboard modifer key or alpha numeric key via Applescript?
For example can Applescript turn the caps lock key or the option key on and off?


1) Open Keyboard Viewer manually (just to see that the following script works)
2) Run script:

Applescript:


-- example 1: toggling option key of the keyboard
tell application "System Events"
   key down option
   delay 5 -- do here what you need instead
   key up option
end tell

delay 2
-- example 2: toggling shift key of the keyboard
tell application "System Events"
   key down shift -- toogle numeric keys & capitalize letter keys
   delay 5 -- do here what you need instead
   key up shift
end tell

delay 2
-- example 3: hiding the script editor
tell application "System Events" to keystroke "h" using command down

Last edited by KniazidisR (2020-10-11 10:57:58 am)


Model: MacBook Pro
OS X: Catalina 10.15.4
Web Browser: Safari 13.1
Ram: 4 GB

Offline

 

#3 2020-10-12 09:24:23 am

Sepa
Member
Registered: 2020-10-09
Posts: 5

Re: Keyboard modifier key toggle on/off without keyboard input

Thank you very much for your help. The toggle works well.
The idea of the Option key toggle is create the combination key sequence Otion+Click.
While the Option key is down a pointer click takes place.
Is it possible to end the delay after the pointer click?
In other words if the delay is 3 seconds, can de delay stop immediately after the click input?
Let me know if this makes sense?

Offline

 

#4 2020-10-13 09:45:19 pm

KniazidisR
Member
Registered: 2019-03-03
Posts: 1352

Re: Keyboard modifier key toggle on/off without keyboard input

Sepa wrote:


The idea of the Option key toggle is create the combination key sequence Otion+Click.
While the Option key is down a pointer click takes place.
Is it possible to end the delay after the pointer click?
In other words if the delay is 3 seconds, can de delay stop immediately after the click input?
Let me know if this makes sense?


The delay, controlled by the occurrence of a certain event, can be implemented by applying an infinite repeat loop to the following scheme. But what event follows after your mouse click? If, for example, a window appears on the screen under the name "I got hung up", then the script can exit the repeat loop, making sure that such a window has just appeared:

Applescript:


tell application "System Events"
   key down option
   repeat
       delay 0.02
       if window "I got hung up" of process myApp exists then
           exit repeat
       end if
   end repeat
   key up option
end tell

Last edited by KniazidisR (2020-10-13 09:52:41 pm)


Model: MacBook Pro
OS X: Catalina 10.15.4
Web Browser: Safari 13.1
Ram: 4 GB

Offline

 

#5 2020-10-16 09:43:38 pm

Sepa
Member
Registered: 2020-10-09
Posts: 5

Re: Keyboard modifier key toggle on/off without keyboard input

I work with a graphic tablet and Photoshop. I use a stylus pen with a button to trigger the AppleScript. With the AppleScript Option key down, I can copy the area below the pen tip. The delay allows me to position the pen tip over the area that I want to copy. Once I copy, then I must wait for the delay to terminate. It would be useful to terminate the delay as soon as the pen copies the area below the tip.

Also, there is a lag in the process. Once I sample the area using the AppleScript I must wait 3-5 seconds before I can resample using the AppleScript.

Offline

 

#6 2020-10-16 10:19:02 pm

KniazidisR
Member
Registered: 2019-03-03
Posts: 1352

Re: Keyboard modifier key toggle on/off without keyboard input

Sepa wrote:

It would be useful to terminate the delay as soon as the pen copies the area below the tip.


I don't know. Try to clean the clipboard, then check if it receives some picture:

Applescript:


set the clipboard to ""

tell application "System Events"
   key down option
   repeat
       delay 0.02
       try
           set |selection| to (the clipboard as JPEG picture)
           exit repeat -- exits repeat loop only when the clipboard has some picture
       end try
   end repeat
   key up option
end tell

Sepa wrote:

Also, there is a lag in the process. Once I sample the area using the AppleScript I must wait 3-5 seconds before I can resample using the AppleScript.


You can put your process into external repeat loop:

Applescript:


repeat
   display dialog "You want to make new selection?" buttons {"Yes", "No"}
   if button returned of result is "No" then exit repeat
   
   set the clipboard to ""
   tell application "System Events"
       key down option
       repeat
           delay 0.02
           try
               set |selection| to (the clipboard as JPEG picture)
               exit repeat -- exits repeat loop only when the clipboard has some picture
           end try
       end repeat
       key up option
   end tell
   
   delay 5
end repeat

Last edited by KniazidisR (2020-10-17 12:57:40 am)


Model: MacBook Pro
OS X: Catalina 10.15.4
Web Browser: Safari 13.1
Ram: 4 GB

Offline

 

#7 2020-10-17 12:17:02 am

Sepa
Member
Registered: 2020-10-09
Posts: 5

Re: Keyboard modifier key toggle on/off without keyboard input

That is an interesting idea, to clear the clipboard first. Then when the stylus copies to the clipboard end the delay. It makes sense but I run into trouble when I run the script.

When I run the AppleScript the OSX gets caught in a loop and it is not able to end the task. The option key eye target stays on as if the option key was permanently in the down position. The only way to exit is by restarting the computer.

Offline

 

#8 2020-10-17 12:55:45 am

KniazidisR
Member
Registered: 2019-03-03
Posts: 1352

Re: Keyboard modifier key toggle on/off without keyboard input

It was typo in my last script.

set |selection| to (the clipboard) as JPEG picture

should be

set |selection| to (the clipboard as JPEG picture)

I updated my last script


Model: MacBook Pro
OS X: Catalina 10.15.4
Web Browser: Safari 13.1
Ram: 4 GB

Offline

 

#9 2020-10-23 07:29:02 pm

Sepa
Member
Registered: 2020-10-09
Posts: 5

Re: Keyboard modifier key toggle on/off without keyboard input

Thank you for your help.

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)