Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feature]: Send keys without activating the app. #67

Closed
Tekl opened this issue Sep 25, 2023 · 2 comments
Closed

[feature]: Send keys without activating the app. #67

Tekl opened this issue Sep 25, 2023 · 2 comments
Labels
enhancement New feature or request

Comments

@Tekl
Copy link

Tekl commented Sep 25, 2023

What feature would you like to see in sendkeys?

It would be nice to send keys to an app which is in the background without activating it.

Other information:

Here's an example swift script that sends Cmd+Up to the Roon app.

#!/usr/bin/env swift
import Cocoa

func sendShortcutToProcess(bundleIdentifier: String, keyCode: CGKeyCode, modifierFlags: CGEventFlags) {
    if let targetProcess = NSRunningApplication.runningApplications(withBundleIdentifier: bundleIdentifier).first {
        let pid = targetProcess.processIdentifier
        guard let eventSource = CGEventSource(stateID: .hidSystemState) else {
            print("Failed to create event source")
            return
        }
        
        let keyDownEvent = CGEvent(keyboardEventSource: eventSource, virtualKey: keyCode, keyDown: true)!
        keyDownEvent.flags = modifierFlags
        keyDownEvent.postToPid(pid)
        
        let keyUpEvent = CGEvent(keyboardEventSource: eventSource, virtualKey: keyCode, keyDown: false)!
        keyUpEvent.flags = modifierFlags
        keyUpEvent.postToPid(pid)
    }
}

// Usage: Call the function with the desired bundle identifier, key code, and modifier flags
sendShortcutToProcess(bundleIdentifier: "com.roon.Roon", keyCode: 126, modifierFlags: .maskCommand)
@socsieng
Copy link
Owner

socsieng commented Oct 6, 2023

Thanks for the feedback.

It should now be possible to achieve this behaviour by including the following flags --targeted and --no-activate

Example:

sendkeys --application-name iTerm2 --targeted --no-activate --characters 'ls<c:enter>'

@Tekl
Copy link
Author

Tekl commented Nov 20, 2023

Thanks a lot for implementing this so quickly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants