C# MouseKeyHook key suppression problems -
i've been using mousekeyhook nuget package project single key press needs monitored provide explicit functionality. prototype application have written, checks required key pressed , sets handled property true. key i'm testing launchapplication2, problem i'm having key press isn't surpressed, @ moment if microsoft word or excel in focus calculator launches!
the code follows:
/// <summary> /// interaction logic mainwindow.xaml /// </summary> public partial class mainwindow : window { private ikeyboardmouseevents keyboardhooklistener; private solidcolorbrush inactivebrush = new solidcolorbrush(colors.white); private solidcolorbrush activebrush = new solidcolorbrush(colors.lightgreen); private bool pressed = false; public mainwindow() { initializecomponent(); this.background = inactivebrush; this.keyboardhooklistener = hook.globalevents(); this.keyboardhooklistener.keydown += keyboardhooklistener_keydown; this.keyboardhooklistener.keyup += keyboardhooklistener_keyup; } void keyboardhooklistener_keyup(object sender, system.windows.forms.keyeventargs e) { if (e.keydata == system.windows.forms.keys.launchapplication2) { if (pressed) { this.background = inactivebrush; this.displaylabel.content = string.empty; this.pressed = false; system.diagnostics.debug.writeline("*********finished*********"); } } } void keyboardhooklistener_keydown(object sender, system.windows.forms.keyeventargs e) { // filter specific buttons using keydata property of event arguments. if (e.keydata == system.windows.forms.keys.launchapplication2) { e.handled = true; e.suppresskeypress = true; // use flag stop code executing multiple times whilst key pressed keydown keeps firing. if (!pressed) { system.diagnostics.debug.writeline("*********started*********"); this.pressed = true; this.background = activebrush; this.displaylabel.content = e.keydata.tostring(); } } } private void window_closing(object sender, system.componentmodel.canceleventargs e) { this.keyboardhooklistener.keydown -= this.keyboardhooklistener_keydown; this.keyboardhooklistener.keyup -= this.keyboardhooklistener_keyup; this.keyboardhooklistener.dispose(); } }
i've tried using suppresskeypress property doesn't have effect. explanations or proposals fix great!
Comments
Post a Comment