Send a key code to an application without activating it first?

前端 未结 5 555
野性不改
野性不改 2020-12-24 11:59

I\'m trying to send the spacebar key to an application without activating it first. The following code almost does what I want but it brings the application to the foregroun

相关标签:
5条回答
  • 2020-12-24 12:30

    How about deactivating it afterwards?

    activate application "X"
    tell application "System Events" to key code 49
    activate me
    
    0 讨论(0)
  • 2020-12-24 12:33

    Position is offscreen to prevent it from flashing where the user can see it or reduce its opacity.

    0 讨论(0)
  • 2020-12-24 12:38

    I don't think you can send a keystroke to an inactive application, but you can hide an app immediately after activating it and executing code for it. This does however, cause the app to briefly flash before it hides.

    tell application "System Events"
        tell application "X" to activate
        key code 49
        set visible of process "X" to false
    end tell
    
    0 讨论(0)
  • 2020-12-24 12:40

    Sending a keystroke can basically be seen as using a keyboard, but the only difference is that the keys that need to be pressed are already predefined. The rest of the process revolving around this doesn't change. This means that the application itself still needs to be opened and activated before you can actually send keystrokes to it.

    Depending on the application however, it might be possible to use certain Applescript functions in the application's API to send different inputs to the application without having to activate it first. Take the Messages API for instance:

    tell application "Messages"
          set theBuddy to buddy "someone@mac.com" of service "iMessage"
          send "Hi there" to theBuddy
    end tell
    
    0 讨论(0)
  • 2020-12-24 12:47

    There are courses of investigation.

    1. If the application is one you have developed yourself you have the following options:

      *simply have a public property exposed and set that to the key you want to send it. *have your application polling a folder for a file and you send your instructions via that.

    2. With a windows API hook of some type you can get control of the application without activating it. I am pretty certain if I put my mind to it I could take control of anything on the computer.

    In simple terms think out of the box, it does not necessarily need to be a key press you send, you just want to instruct it to do something. There is loads of options Interface Marshalling, Interops, OLE, DDE, looks like I have turned up on this site just in time!

    0 讨论(0)
提交回复
热议问题