gumenimeda
gumenimeda

Reputation: 835

Get input characters typed by the user(WINAPI, user32.dll)

I need to get an input point (caret position, window/control) that is focused. My application/service needs to detect when the user starts typing and then replace the characters that were typed with predetermined values. The trick is that I don't know where the user wants to type(I don't want to limit this to several applications).

I think that I know how to get/replace the text using:

[DllImport("USER32.DLL", CharSet = CharSet.Auto)]
public static extern IntPtr SendMessage(IntPtr hWnd, uint msg, IntPtr wparam, StringBuilder text);

How do I get the control that is focused? How do I know from where to get the text and where to send the replacement?

I am doing this in C#, WPF.

Thank you!

Upvotes: 0

Views: 1498

Answers (2)

Ben Voigt
Ben Voigt

Reputation: 283674

If you just want to emulate keyboard activity, you can use keybd_event or SendInput. These automatically deliver the keypresses to the window with focus, so you don't have to detect it yourself.

Upvotes: 2

Mark Hall
Mark Hall

Reputation: 54532

I am presuming that since you have tagged your question as WinAPI your are trying to intercept keystrokes from another application as in Keyboard Hooking. Take a look at these Links:

Upvotes: 1

Related Questions