Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
663 views
in Technique[技术] by (71.8m points)

windows - How can I fire a key press or mouse click event without touching any input device at system level?

How can I fire an automatic key press or mouse click event when a color appears on the screen on other application or browser?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

It depends a lot on what you want. Do you want to send the keys to

  • your Application
  • another fixed Application
  • Simulate a global keypress

Simulating keys globally

All of these will cause problems targeting a specific application and the active window changes.

  • SendKeys Sends Messages to the active app. It's a high level function taking a string which encodes a sequence of keys.

  • keybd_event is very low level and injects a global keypress. In most cases SendKeys is easier to use.

  • mouse_event simulates mouse input.

  • SendInput supersedes these functions. It's more flexible but a bit harder to use.

Sending to a specific window

When working with a fixed target window, sending it messages can work depending on how the window works. But since this doesn't update all states it might not always work. But you don't have a race condition with changing window focus, which is worth a lot.

  • WM_CHAR sends a character in the basic multilingual plane (16 bit)
  • WM_UNICHAR sends a character supporting the whole unicode range
  • WM_KEYDOWN and WM_KEYUP Sends keys which will be translated to characters by the keyboard layout.

My recommendation is when targeting a specific window/application try using messages first, and only if that fails try one of the lower level solutions.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...