BlackCath
BlackCath

Reputation: 826

C# Copy text in a textbox where focus is

I have this problem and don't know where to start. I need to write a program that will run in WinCE, so Compact Framework will be required, and this program has to write text (a string) wherever the cursor is. I mean, if my program is running, and the cursor is in a notepad window. the text must be displayed in notepad. Or if the cursor is in another application inside a textbox (or textfield if that app was wrote in java), the text must be placed in that textbox.

I'm able to do all the other functionality the program requires, but I don't know how to do this. As far as I have read, it is done with API's. And that is all I can understand.

Any help will be apreciated.

Thanks!

Upvotes: 1

Views: 836

Answers (2)

Damon8or
Damon8or

Reputation: 527

We use P/Invoke keybd_event to generate keyboard entry. The only other approach I can think of would be to put your string in the clipboard, and generate a paste key stroke. I'm not sure every app you encounter is going to respond to CTRL+V the same. Perhaps there's a way to trigger the paste programmatically from your app?

[DllImport("coredll.dll", EntryPoint = "keybd_event", SetLastError = true)]
public static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);

This should generate a silent A keystroke.

keybd_event((byte)Keys.A, 0, 0, 0x0004);
keybd_event((byte)Keys.A, 0, 0x0002, 0x0004);

Upvotes: 0

RQDQ
RQDQ

Reputation: 15569

The first thing that comes to mind is SendKeys. It's a simple way to emulate typing.

Upvotes: 2

Related Questions