robotgo icon indicating copy to clipboard operation
robotgo copied to clipboard

[Feature Request] Send keys to minimized or inactive windows

Open smalleel opened this issue 7 years ago • 17 comments

  • Robotgo version (or commit ref): 0.48.0
  • Go version: go 1.10
  • Gcc version: 7.3.0
  • Operating system and bit: Windows 10 64bit / Linux manjaro 4.14.27 64bit
  • Can you reproduce the bug at Examples:
    • [ ] Yes (provide example code)
    • [ ] No
    • [x] Not relevant

Description

Is there a workflow available for sending keystrokes to background or inactive windows? Keystrokes sent with KeyTap() or TypeString() trigger the key events fine, and SetActive() can work to bring a window to the front to receive keystrokes, but I was wondering is there any way to send keys to an inactive or minimized window.

There seem to be some ways to do this with C# on Windows per this SO question's solutions. I'm not sure what complexity is involved with other OSes or with Golang though.

Is sending keys to minimized or inactive windows something that could ever be supported by Robotgo? Is there already a way that I missed?

smalleel avatar Mar 24 '18 03:03 smalleel

Currently this function is not supported.

vcaesar avatar Mar 24 '18 12:03 vcaesar

Thanks for confirming! If it is possible, it would be nice to see this feature built into robotgo someday. :)

smalleel avatar Mar 25 '18 03:03 smalleel

I agree. This feature would be nice to have.

gokomer avatar May 01 '18 13:05 gokomer

I actually came here looking for this. Anyone mind if I try implementing this or know if any work has been done yet? I'm thinking adding a func KeyToggleWindow(pid int32, args ...string) string (and related functions just with window at the end) that would send keys to the window with the given pid

Noofbiz avatar Nov 26 '18 06:11 Noofbiz

For windows we'd use PostMessageA instead of SendInput https://docs.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-postmessagea or maybe PostThreadMessage to get info needed to wait and see if the message succeeded

Noofbiz avatar Nov 26 '18 06:11 Noofbiz

Looks like cocoa can use CGEventPostToPSN instead of CGEventPost https://stackoverflow.com/questions/21878987/mac-send-key-event-to-background-window

Noofbiz avatar Nov 26 '18 19:11 Noofbiz

@Noofbiz Thanks for your suggest.

vcaesar avatar Nov 26 '18 23:11 vcaesar

Hmm it looks like for macOS you need accessibility permissions to send events to background apps. Just using CEEventPostToPID isn't enough. I'll see if I can get it working by building the app with the right permissions in XCode. It doesn't work by just running 'go run' in the terminal. It still sends the events only to the active window.

Noofbiz avatar Dec 01 '18 16:12 Noofbiz

The problem I'm having with Mojave appears to be an issue with the new app sandboxing seen here https://stackoverflow.com/questions/51299066/macos-mojave-automator-not-authorized-to-send-apple-events-to-system-event. Not sure how to work around it at the moment

Noofbiz avatar Dec 03 '18 02:12 Noofbiz

Very strongly concerned about this feature!!!

qwenode avatar Aug 16 '20 18:08 qwenode

This would be awesome! I'm trying to write something to help multi boxing in World of Warcraft ( which is fine by their ToS and a practice many people do ) but need to be able to send keystrokes to non-active windows to get it working properly

roffe avatar Oct 10 '20 16:10 roffe

And capturing screenshot of background window, looking forward to it :)

aj3423 avatar Jan 14 '22 17:01 aj3423

Hmm it looks like for macOS you need accessibility permissions to send events to background apps. Just using CEEventPostToPID isn't enough. I'll see if I can get it working by building the app with the right permissions in XCode. It doesn't work by just running 'go run' in the terminal. It still sends the events only to the active window.

FYI, I know this is an old post, but for me, on BigSur the CGEventPostToPid worked! (Just need enough delay or it won't get detected properly.) (I have added a PR to a different keyboard lib with that: https://github.com/micmonay/keybd_event/pull/37/files I used it to control OBS in the background.)

Gerifield avatar Feb 17 '22 09:02 Gerifield

@Gerifield Thx, I will think about it.

vcaesar avatar Feb 17 '22 18:02 vcaesar

Added it in the mac os, are there have PR in other platform?

vcaesar avatar Apr 08 '22 21:04 vcaesar

I'm a Windows user and I'm looking forward to this feature

bymomo avatar Aug 03 '22 07:08 bymomo

I'm a Windows user and I'm looking forward to this feature

BorgLee avatar Oct 24 '22 02:10 BorgLee