Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can Python be used to send a true key down event to Mac

My question is, is there any way to reliably hold a key down on a mac with applescript/python?

I have search almost everywhere for topics like this, however, none of them cut it. I am attempting to use Python to read serial information from an Arduino, then relay those signals as keypresses. I have seen how to use applescript to send a "key down" to the system event, as shown in the following code:

(Python code)
    def SendDown(key):
    string = str(key)
    cmd = """osascript -e 'tell application "System Events" to key down (key code """ + string   + ")'"
    os.system(cmd)

This code works generally, however, I want to control the Google flight simulator. When I attempt to do this, the key presses seem to be way to quick and the flight simulator or the basic google earth map moves fractions of what I would expect.

They way I am using this code is essentially as follows (suedocode)

if (ArduinoMessage == "left"):
    SendDown(leftKey) #leftKey has been set to 123 -- the code for the left arrow key
    etc...

From my point of view, the key down event I'm sending is essentially a quick keystroke and the key is not being held down. I tried programming the key event directly in applescript and had a little bit of success. My code looked something like this:

tell application "System Events"
    repeat 50 times    
        key down (key code 123)
    end repeat
    key up (key code 123)
end tell

That code moved the google earth map more than I have been getting, but it took a long time to get it to move a small amount (far less than the normal arrow keys). Then I tried to write that applescript into Python and lost all improvement.

So I restate the question -- is there any way to reliably hold a key down on a mac with applescript/python?

I was able to get this to work on Windows fairly easily, however, I was able to use a Windows only library called SendKeys that is designed for applications such as this.

Any help would be appreciated.

Thanks,

Jake

like image 428
Jake Avatar asked Jul 09 '13 04:07

Jake


People also ask

How do I capture a keyboard event in Python?

We can also detect keypress using the wait() function defined in the keyboard module. The wait() function takes a character as input. Upon execution, it keeps waiting until the user presses the key passed as an input argument to the function. Once the user presses the right key, the function stops its execution.

Does Mac Have a Python compiler?

Python comes pre-installed on Mac OS X so it is easy to start using. However, to take advantage of the latest versions of Python, you will need to download and install newer versions alongside the system ones.

How do you read a key in Python?

Using the keyboard module to detect keypress in Python The read_key() function from this module is used to read the key pressed by the user. We can check whether the pressed key matches our specified key.


1 Answers

Use PyUserInput. Try following code.

import time

import pykeyboard


# TODO: Replace following two lines with the code that activate the application.
print('Activate the application 3 seconds.')
time.sleep(3)

k = pykeyboard.PyKeyboard()
k.press_key(k.left_key)
time.sleep(1) # Hold down left key for 1 second.
k.release_key(k.left_key)

Unfortunately I don't have Mac. I tested in Linux, Windows.

like image 98
falsetru Avatar answered Sep 27 '22 18:09

falsetru