Hi,
i would like that when pressing the key example "M" to call some code. Same as Ctrl+Alt+Delete and TaskManager but in my case i have process in background which maybe can be used for this?

Dani AI

Generated

Short answer for : don’t rely on form KeyDown (it only fires when your window is active) and you probably don’t need a low-level global hook. Use the Win32 RegisterHotKey API to bind a system-wide hotkey that still sends a WM_HOTKEY message when your app is minimized (tray or hidden). That avoids DLL injection/global-hook complexity (what and hinted at) and is simpler to maintain.

Key points to implement

  • Register a hotkey (usually with modifiers, e.g. Ctrl+Alt+M) so you avoid conflicting with normal typing. Plain M without modifiers will interfere with user input and other apps.
  • Handle WM_HOTKEY in your window’s message loop (override WndProc) and call your auto-click routine from there.
  • Unregister the hotkey on exit. If your form is hidden, ensure its window handle exists before you register (access Handle to force creation) or create a message-only window/HwndSource for the messages.
  • You cannot intercept OS Secure Attention Sequence (Ctrl+Alt+Del). Services and non-interactive sessions cannot receive desktop hotkeys either.

A minimal C# WinForms pattern

const int WM_HOTKEY = 0x0312;
const int HOTKEY_ID = 9000;
const uint MOD_CONTROL = 0x0002;
const uint MOD_ALT = 0x0001;

[DllImport("user32.dll", SetLastError=true)]
static extern bool RegisterHotKey(IntPtr hWnd, int id, uint fsModifiers, uint vk);
[DllImport("user32.dll", SetLastError=true)]
static extern bool UnregisterHotKey(IntPtr hWnd, int id);

// in OnLoad:
RegisterHotKey(this.Handle, HOTKEY_ID, MOD_CONTROL | MOD_ALT, (uint)Keys.M);

// in WndProc:
if (m.Msg == WM_HOTKEY && m.WParam.ToInt32() == HOTKEY_ID) StartAutoClick();

// on close:
UnregisterHotKey(this.Handle, HOTKEY_ID);

Troubleshooting: check RegisterHotKey’s return value and inspect Marshal.GetLastWin32Error() on failure; choose modifiers to avoid collisions; avoid global hooks unless you specifically need to inspect every keystroke (they’re more complex and can trigger AV warnings).

Recommended Answers

All 8 Replies

I know for key down events. I guess I need to use a DLL for this. Program form is not active to use keydown or any similar events that is the problem. Same as when the calls Task Manager with Ctrl+Alt+Delete.

So you would lke to have something like a key logger then?

Why do all think of some lame tools :P? Give more details then... I have application which has the function with automatic click. When press a key, then should call some code to start the process of automatic click.

Can you elaborate on automatic click ? If you think my native language is English, you are wrong, I am just trying to do my best in it.
I understand what a click is. Is it a mouse click? Or the click sound of a keyboard key, please tell me. And you have an application which does this automaticaly? Please explain...

You might want to take a look at this thread: http://www.daniweb.com/forums/thread215125.html

It's a little lengthy, but if you don't find what you need in there at least it might help to explain better what you are looking for.

I made a simple application that automatically simulate mouse click. I want to run a function that simulates just automatically click of the mouse by pressing a key "M". The problem is that my application is minimized to be performed automatically click mouse. Principle is the same as for TaskManager with Ctrl+Alt+Delete just what to call this part of the code and not the form.

There is a link to this article in the thread DdoubleD posted. It shows you what you need to do. It is a global hook, that will recieve the mouse/keyboard events from windows.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.