Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Application does not accept keystroke

I am trying to send keystrokes to the application VisualBoyAdvance using AppleScript, but I cannot get it to work.

My code, so far, is this:

tell application "VisualBoyAdvance"
    activate

    tell application "System Events"
        keystroke "k"
    end tell

end tell

When I tell VisualBoyAdvance directly, I get this error:

error "VisualBoyAdvance got an error: Can’t get keystroke \"k\"." number -1728 from keystroke "k"

I have tried telling VisualBoyAdvance directly, and I have also tried using key code 40, but I still cannot get it to work. Strangely enough, this does work:

tell application "VisualBoyAdvance"
    activate

    tell application "System Events"
        keystroke "d" using {command down}
    end tell

end tell

But that is a keyboard shortcut that shows up in the menu bar, so I guess it would be a bit different.

How can I use AppleScript to simulate a keypress and make the application respond to it? If I cannot use AppleScript for this, what else could I use?

like image 861
Josh Hunt Avatar asked Mar 01 '10 03:03

Josh Hunt


People also ask

What does it mean to receive KeyStrokes?

Keystroke logging, often referred to as keylogging or keyboard capturing, is the action of recording (logging) the keys struck on a keyboard, typically covertly, so that a person using the keyboard is unaware that their actions are being monitored. Data can then be retrieved by the person operating the logging program.

What is KeyStrokes on Mac?

KeyStrokes enables you to use a mouse, trackball, head pointer or other mouse emulator to type characters into any standard Mac OS X application.


1 Answers

I think you're almost there. Here's something I used for Safari; in this example, I send key code 48 (tab).

tell application "Safari"
    activate

    tell application "System Events" to tell process "Safari" to key code 48
end tell

AFAICS this ought to be largely independent of AppleScript support in the target process as you're asking System Events to simulate the key press via Universal Access.

For help with key codes, see this useful application: http://manytricks.com/keycodes/

like image 97
Andrew Hodgkinson Avatar answered Nov 03 '22 00:11

Andrew Hodgkinson