Capturing Mouse/Keyboard Events Outside of Form (App Running in Background)

Capturing mouse/keyboard events outside of form (app running in background)

The magic words are windows hooks. These are created with a p/invoke call to SetWindowsHookEx. You can set up a hook to monitor, among others, keyboard and mouse events. Normally, such hooks are local to the application, but you can also create global hooks. The Microsoft KB shows how.

However, be aware that not all types of global hooks can be used from .NET. In particular, there are only two that you can use: the low-level keyboard and mouse hooks, known as WH_KEYBOARD_LL and WH_MOUSE_LL. Luckily, these are just what you need.

Java Keyboard/Mouse activity (even outside of my app)

Since JNI is required, which relys on the OS specific APIs, the only way would be to
have an implementation for each supported OS and load it dynamically after detecting the OS in you application.

For Windows
Java Global Keyboard/Mouse Hook – JNI

logging keyboard activity from a background process

I think what you are looking for is described in this article. The code installs and uninstalls the hook when your application starts/ends and calls Console.WriteLine((Keys)vkCode) every time a key is pressed. You can change that point in the code; vkCode contains the virtual key code of the pressed key. You can convert it by casting it to System.Windows.Forms.Keys and then using System.Windows.Forms.KeysConverter.

Not able to get the mouse click event

This is often referred as global mouse hook.

Here's an example: http://www.codeproject.com/Articles/7294/Processing-Global-Mouse-and-Keyboard-Hooks-in-C

The associated project on codeplex: http://globalmousekeyhook.codeplex.com/

Using doubleclick to catch x,y coordinates outside of a form C#

This old MSDN blog has sample code for using WH_MOUSE_LL, a low-level mouse hook that you can use to capture mouse events in Windows. Mouse hooks do not distinguish double clicks however, you will need to do that yourself. You can use SystemInformation.DoubleClickTime and a timer to determine if the click was a double click or not.

wxPython - Trapping Mouse & Keyboard Events without window Focus

Do you really need mouse and keyboard events or would it be sufficient to just know if the user has been idle? (You mentioned a time management app, so this seems feasible.)

This code will work on Windows and returns the idle time in seconds.

from ctypes import Structure, windll, c_uint, sizeof, byref

class LASTINPUTINFO(Structure):
_fields_ = [
('cbSize', c_uint),
('dwTime', c_uint),
]

def get_idle_duration():
lastInputInfo = LASTINPUTINFO()
lastInputInfo.cbSize = sizeof(lastInputInfo)
windll.user32.GetLastInputInfo(byref(lastInputInfo))
millis = windll.kernel32.GetTickCount() - lastInputInfo.dwTime
return millis / 1000.0

How can I capture mouse events that occur outside of a (WPF) window?

I believe you are reinventing the wheel. Search for "Window.DragMove".

Example:

    private void title_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
this.DragMove();
}


Related Topics



Leave a reply



Submit