Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

windows

events

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

like image 249
Javed Akram Avatar asked Oct 30 '10 07:10

Javed Akram


People also ask

Which event is called when key is pressed in Javascript?

The onkeypress event occurs when the user presses a key (on the keyboard).

Which key is pressed Windows?

Pressing Win in combination with other keys will access shortcuts ; these will vary based on the version of Windows you are using. If your keyboard does not have a Windows key, you can access the Start menu, but not other shortcuts, by pressing Ctrl-Esc .


2 Answers

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.

like image 80
CodesInChaos Avatar answered Sep 28 '22 02:09

CodesInChaos


when a color appears on the screen on other application or browser

I made one program using OpenCV and C++ for operating mouse with finger gesture. I used 3 color strips for 3 mouse function.

  • Yellow color for Left click
  • Blue color for Right click
  • Pink color for controlling cursor position

Whenever camera detect these colors, associated function takes place, I have used mouse_event for performing mouse function. For more information you may read my code, blog, video.

like image 37
udit043 Avatar answered Sep 28 '22 03:09

udit043