c# - KeyEventArgs to Timer.Tick -
i try keyeventargs tick. if thinks why i'm using timer, needs detect keypress outside of window too.
i don't have "error" in code, when run program comes:
exception unhandled. system.nullreferenceexpetion: (i try translate) 'your object referral can't define object occurrence'
now thing i'm using is
private void form1_load(object sender, eventargs e) { a.start(); a.interval = 1; } private void a_tick(object sender, eventargs e) { keyeventargs ke = e keyeventargs; if (ke.keycode == keys.r) { test = true; } }
i need help, because error has been long time. thanks
from looking @ way put question me seems you're trying detect key press using timer. instead can use form's keypress event.
use:
private void form1_keypress(object sender, keypresseventargs e) { if (e.keycode == keys.something) { //do } }
don't forget set keypreview = true;
form.
edit: if need detect keypresses without focus(outside of form), need global hotkey hooking. need following:
first include this:
using system.runtime.interopservices;
you need @ top of class:
[dllimport("user32.dll")] public static extern bool registerhotkey(intptr hwnd, int id, int fsmodifiers, int vlc); [dllimport("user32.dll")] public static extern bool unregisterhotkey(intptr hwnd, int id); const int hotkey = 1;
and need call inside form's load:
registerhotkey(this.handle, hotkey, (uint)modifierkeys.something, (uint) keys.something);
then need handle press:
protected override void wndproc(ref message m) { if (m.msg == 0x0312 && m.wparam.toint32() == hotkey) { //do when pressed } base.wndproc(ref m); }
wiki
Comments
Post a Comment