Notice: This forum has been recovered from an old backup, so some content, links, and dates may be outdated. The forum is currently read-only while we restore sign-in and registration functionality. Details

If you find this forum valuable and would like to help keep it online, donations to help cover hosting and domain costs are greatly appreciated, but never expected. You can support the forum through Buy Me a Coffee or Ko-fi. Thank you for helping preserve the EventGhost community.

Sending keystrokes to a program

If you have a question or need help, this is the place to be.
Post Reply
jachin99
Experienced User
Posts: 614
Joined: Sat Feb 13, 2016 8:39 pm

Sending keystrokes to a program

Post by jachin99 »

I am looking for a way to construct a macro that sends a keystroke to an open program. I would like to do this without causing anything flashing on the screen, and without using a series of find window, bring window to front, etc. macros. I have come across different sites giving advice, such as using send keys or send input etc. but I never really understood them. Is there a way to construct a python script in EG that would send a particular keystroke to an open program without messing around with grabbing windows or having batch command boxes popping up? thanks.
blaher
Experienced User
Posts: 487
Joined: Thu Nov 17, 2011 1:27 am

Re: Sending keystrokes to a program

Post by blaher »

jachin99 wrote:sends a keystroke to an open program
Which open program? That's where the 'find window' comes in handy, but you could use task monitor plugin, to enable/disable your macro instead.
jachin99
Experienced User
Posts: 614
Joined: Sat Feb 13, 2016 8:39 pm

Re: Sending keystrokes to a program

Post by jachin99 »

I have the insert key programmed to turn off my TV, and stereo through JRiver Media Center's Remote control feature. I used insert because It has a power logo on the physical key on my keyboard. JRiver itself runs all of the time so enabling a macro only when this one program is running would not do me any good. Instead, i was thinking i would write a script in either python or c that sent JRiver an insert keystroke, and trigger that script using a button on a MCE remote. My problem is I don't know how to script it, aside from using a batch script maybe. Batch scripts display a box on the screen, even when using @echo off. I am looking for a way to do this without causing any popups.
User avatar
kgschlosser
Site Admin
Posts: 5190
Joined: Fri Jun 05, 2015 5:43 am
Location: Rocky Mountains, Colorado USA

Re: Sending keystrokes to a program

Post by kgschlosser »

this can be done without having to activate the window. but you need to know the specific control in the application you want to send the keystrokes to.


so you would use the EG find window and target the specific control you want to send the keystrokes to


and then in a python script in the same macro as the find window

Code: Select all

KEYSTROKES = "whatever it is you want to send"
import win32api
import win32con

win32api.PostMessage(eg.result[0], win32con.WM_CHAR, KEYSTROKES, 0)
If you like the work I have been doing then feel free to Image
User avatar
kgschlosser
Site Admin
Posts: 5190
Joined: Fri Jun 05, 2015 5:43 am
Location: Rocky Mountains, Colorado USA

Re: Sending keystrokes to a program

Post by kgschlosser »

and the reason why you have to use find window is because the "window id" changes each and every time the program gets run. and the find window is already tasked and made to do all of the work of locating that id. the use of it does not cause anything to pop up at all. and by using the above mentioned script we will use that id to directly send the keystrokes right into that control not needing to activate the window and bring it into focus to use the keyboard emulator which does not know how to target anything but the active window. this is a nice upgrade to add to the keyboard emulator tho. I would have to find a nifty way to have the handle passed between the 2 if used in succession.

maybe just adding a check box to the keyboard emulator action indicating that you want to use the window that is found by find window instead of the active window
If you like the work I have been doing then feel free to Image
jachin99
Experienced User
Posts: 614
Joined: Sat Feb 13, 2016 8:39 pm

Re: Sending keystrokes to a program

Post by jachin99 »

So in your script, I would have to substitute the Win32API & Win32Con calls with calls to JRiver's input API?
User avatar
kgschlosser
Site Admin
Posts: 5190
Joined: Fri Jun 05, 2015 5:43 am
Location: Rocky Mountains, Colorado USA

Re: Sending keystrokes to a program

Post by kgschlosser »

jachin99 wrote:So in your script, I would have to substitute the Win32API & Win32Con calls with calls to JRiver's input API?

No... all you have to do is change the KEYSTROKES = "" to whatever key it is you want to send. and place the action Find Window before it. and target the Find Window to the jriver control.

so you would have basically this in your tree

Code: Select all


macro - some macro name
    event - Some.Event
    action - Find Window, JRiver control (this may just be the JRiver UI in general)
    action = Python Script, Code from the other post with the KEYSTROKES =  change to whatever you want to send

the only thing is you have to make sure there is only one window found. because the code below will onlt send the command to the first item.. so if there are 10 windows or controls found that meets your search parameters then it will only send the keystrokes to the first one in the list of them.
If you like the work I have been doing then feel free to Image
jachin99
Experienced User
Posts: 614
Joined: Sat Feb 13, 2016 8:39 pm

Re: Sending keystrokes to a program

Post by jachin99 »

KG, thanks for your help.
User avatar
kgschlosser
Site Admin
Posts: 5190
Joined: Fri Jun 05, 2015 5:43 am
Location: Rocky Mountains, Colorado USA

Re: Sending keystrokes to a program

Post by kgschlosser »

I do not know if that code worked. but maybe it pointed you in the right direction.
If you like the work I have been doing then feel free to Image
jachin99
Experienced User
Posts: 614
Joined: Sat Feb 13, 2016 8:39 pm

Re: Sending keystrokes to a program

Post by jachin99 »

I stopped using JRiver but i might be able to use this for something else later on.
User avatar
kgschlosser
Site Admin
Posts: 5190
Joined: Fri Jun 05, 2015 5:43 am
Location: Rocky Mountains, Colorado USA

Re: Sending keystrokes to a program

Post by kgschlosser »

what did ya change to???

I am running media portal.. well kind of... it's highly customized.
If you like the work I have been doing then feel free to Image
jachin99
Experienced User
Posts: 614
Joined: Sat Feb 13, 2016 8:39 pm

Re: Sending keystrokes to a program

Post by jachin99 »

I ended up getting a two Win 7 licenses and converting all of my PCs back to Win 7 to run WMC so that if my cable company ever flags my channels for DRM, I can still play and record TV, and its wife friendly. I also use emby because it plugs into WMC, runs MadVR, and it has a CSS editor within the program. Windows Media center isn't quite as dead as it looks though, and I have been able to restore most of its original functionality. JRiver was nice but I don't use most of what it has to offer, and I figure if I ever need its audio features, I can do a minimal install, and route my sound through its audio engine without using tagging, or any of its UI. I've tried Media Portal, and it looked pretty simple also though.
User avatar
kgschlosser
Site Admin
Posts: 5190
Joined: Fri Jun 05, 2015 5:43 am
Location: Rocky Mountains, Colorado USA

Re: Sending keystrokes to a program

Post by kgschlosser »

See the reason why I like media portal is because the TV end of it is a true server model. Which works great because if you have a network attached tuner setup like I do (6 tuners) it will handle deligation of who gets to use what tuner and when. And it works it around any recordings. WMC is a point to point setup. So if you have a recording going on from one computer and on another you go to watch a TV program. But it will go and Shanghai a tuner. Doesn't matter if something is recording or if someone else is watching it. And also because of the server based setup. I use a ramdrive for all of the time shifting storage. As I need this to work extremely fast because of 6 tuners at high def but also to not trash my ssd's so I only had to worry about loading up one machine performance wise. And media portal has a nice 10 foot interface if you use Titan. The only downside is the setup. It's a royal pain. But once you get it going it's good.. mine has been trucking away for over a year now. Without issue (well no issues that I haven't caused)
If you like the work I have been doing then feel free to Image
Post Reply