Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Send keystrokes from a C(++) program to a GUI app ?

293 views
Skip to first unread message

R.Wieser

unread,
May 17, 2018, 2:03:36 PM5/17/18
to
Hello all,

I've got a program which receives instructions from an IR remote, and I
would like to pass those instructions on, as keystrokes, to a specific
window*. But although I did find several "its easy to do in Python" and
"this is how you create a keylogger" references, I could not even find a
lead to how to do it in C / C++ **.

*Ultimatily I would check what the active app is, and from it select the
instruction-to-keystroke mapping in my program. (I've also encountered LIRC,
but I've not seen any info in regard to either sending the learned "keys"(?)
to an app or doing dynamic translation).

** I found several commandline tools, but that doesn't really help (shelling
from my program to external tools is not really my thing).

tl;dr:
does anyone know how to "sendkeys" in C/C++ to a GUI app.

Regards,
Rudy Wieser


Björn Lundin

unread,
May 17, 2018, 5:17:22 PM5/17/18
to
On 2018-05-17 20:03, R.Wieser wrote:
> ** I found several commandline tools, but that doesn't really help (shelling
> from my program to external tools is not really my thing).
>
> tl;dr:
> does anyone know how to "sendkeys" in C/C++ to a GUI app.

Can't you look in the source for those programs ? That would give you a
clue?


--
--
Björn

Martin Gregorie

unread,
May 17, 2018, 5:51:06 PM5/17/18
to
I've done something similar when I needed to pass single keystrokes into
a Java program: Java won't read keystrokes from a Linux terminal
application, only a LF-terminated string.

So, I wrote a small C program that tweaked stdin to accept individual
keystrokes and passed them, one by one, to the Java app over a socket
connection. My application echoes received characters back to the
keyreader as a primative form of flow control. The only critical thing
with this approach is that the consuming program needs to be up and
expecting a connection before the key reader is launched. I used a shell
script to deal with startup sequencing:

#!/bin/bash
java MyApp &
sleep 1
keyreader

though I could have just as easily invoked ProcessBuilder.start() after
my application had opened its listener socket - or execve() if it was a C
program.

I don't see why the same trick wouldn't work for any other data source as
well as it does for the keyboard.


--
Martin | martin at
Gregorie | gregorie dot org

R.Wieser

unread,
May 18, 2018, 2:34:04 PM5/18/18
to
Björn,

>> does anyone know how to "sendkeys" in C/C++ to a GUI
>> app.
>
> Can't you look in the source for those programs ? That would give
> you a clue?

:-) My googeling *should* have turned up such sources, but I did not find
anything in that regard. Hence my posting here.

Regards,
Rudy Wieser


R.Wieser

unread,
May 18, 2018, 2:34:04 PM5/18/18
to
Martin,

> and passed them, one by one, to the Java app over a socket
> connection.

The problem with that is that several of the programs I want to control are
already build, and I cannot just change them (think file & webbrowser).

Also, I do not want to control just a single app, I want to be able to
control whatever is on the desktop (hence changing the command-to-keystroke
table, depending on the focussed app)

... But lets beging with just sending keystrokes.

Regards,
Rudy Wieser


mm0fmf

unread,
May 18, 2018, 2:42:47 PM5/18/18
to
Windows or Linux or what?

Björn Lundin

unread,
May 18, 2018, 3:08:58 PM5/18/18
to

The Natural Philosopher

unread,
May 19, 2018, 3:55:36 AM5/19/18
to
Does there exist a 'windows' for rapsberry Pi???


--
“Progress is precisely that which rules and regulations did not foresee,”

– Ludwig von Mises

Andy Burns

unread,
May 19, 2018, 4:01:04 AM5/19/18
to
The Natural Philosopher wrote:

> Does there exist a 'windows' for rapsberry Pi???

Yes ... but ...

<https://blogs.msdn.microsoft.com/teju_shyamsundar/2016/09/06/windows10iotraspberrypi3/>
0 new messages