PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

How to send keyboard input to other running program?

I would like to implement a function like this:
My program is running in the background, and register a hotkey associated with a hardware button. when the button was pressed, my program will send a "Ctrl+C" to the foreground program(for example PocketWord). the key function is like this:

void SendCtrlC()
{
keybd_event(...);
keybd_event(...);
...
Sleep(0);
}

The code works perfect under PPC 2002 emulator, but just won't work under my IPaq! Anyone can give me some hints?
Hello.

#1 Are you sure your function is firing when you hit the button?

#2 Are you sure it's not your program that's not picking up the keyboard event?

#3 The ctrl-c may be mapped to different things in different programs. Try something slightly different, and more "compatible":

Code:


HANDLE lhandle_active;

lhandle_active = GetActiveWindow();
Sendmessage(lhandle_active, WM_COPY,0,0);


... presuming you're really wanting to initiate a copy event. If not you could post the window a WM_KEYPRESS event with the key value as the 3rd parameter.

Cheers,

Refractor
Thanks for the reply.

What I want to do is to copy "Selected text" in *Other* program, not in mines.

#1 My function do get called. It does work in PPC 2002 emulator. And even strange, it works in my IPaq when I debug it by setup breakpoints and step over that function. But it didn't work in my IPaq when it was used normally( without being debuged )

#2 I use Spy++ to check the message event: the target program didn't get the input, neither my program. :(

#3 Sending a WM_COPY is not enough. My program uses several methods to do that. Send a Ctrl-C is only one of them. If you like, we can discuss that in other thread.

and according to MSDN Library, GetActiveWindow "retrieves the window handle to the active window associated with the thread that calls " , So it can't be used to get other program's active window, or focus window. I would prefer to send message to the active window of other program, but seems that no way can get the handle of that under WinCE.
#2 Weird.

#3
Sorry about the GetActiveWindow thing - I didn't read the MSDN page properly :oops:

However, maybe:

HWND GetForegroundWindow(void);

Will give you the active window? It doesn't mention anything about it being thread-specific.

If it doesn't work, let me know and I'll see if it works on mine.

Why isn't a WM_COPY enough?

Cheers,

Refractor
GetForegroundWindow() only give you the top parent window of the active window. You still don't know the exact active window.

#Why WM_COPY is not enough?
1. You need to find the active window handle, but seems no way to do that( Remind me if you have any method to get it).

2. Some controls, ex. HtmlView Control, dosen't support WM_COPY, you need to send it a undocumented message to do that.

Rayman