c# - Program is unresponsive for a couple of seconds -


i found way capture print screen button in c#. when pressing alt + print screen simple messagebox pops saying keys have been pressed. but it's unresponsive couple of seconds right after. have no idea why happening.

here's code use:

static class program {     /// <summary>     /// main entry point application.     /// </summary>     [stathread]     static void main()     {         application.enablevisualstyles();         application.setcompatibletextrenderingdefault(false);         _hookid = sethook(_proc);         application.run(new form1());         unhookwindowshookex(_hookid);       }      /****************************************/     private const int wh_keyboard_ll = 13;     //private const int wh_keyboard_ll = 13;       private const int wm_keydown = 0x0100;     private const int vk_f1 = 0x70;     private static lowlevelkeyboardproc _proc = hookcallback;     private static intptr _hookid = intptr.zero;      private static intptr sethook(lowlevelkeyboardproc proc)     {         using (process curprocess = process.getcurrentprocess())         using (processmodule curmodule = curprocess.mainmodule)         {             return setwindowshookex(wh_keyboard_ll, proc,                 getmodulehandle(curmodule.modulename), 0);         }     }      private delegate intptr lowlevelkeyboardproc(         int ncode, intptr wparam, intptr lparam);      private static intptr hookcallback(         int ncode, intptr wparam, intptr lparam)     {          if (ncode >= 0)         {             keys number = (keys)marshal.readint32(lparam);             if (number == keys.printscreen)             {                 if ((wparam == (intptr)260 && keys.alt == control.modifierkeys && number == keys.printscreen))                 {                     messagebox.show("you pressed alt+ print screen");                 }             }          }         return callnexthookex(intptr.zero, ncode, wparam, lparam);      }      [dllimport("user32.dll", charset = charset.auto, setlasterror = true)]     private static extern intptr setwindowshookex(int idhook,         lowlevelkeyboardproc lpfn, intptr hmod, uint dwthreadid);      [dllimport("user32.dll", charset = charset.auto, setlasterror = true)]     [return: marshalas(unmanagedtype.bool)]     private static extern bool unhookwindowshookex(intptr hhk);      [dllimport("user32.dll", charset = charset.auto, setlasterror = true)]     private static extern intptr callnexthookex(intptr hhk, int ncode,         intptr wparam, intptr lparam);      [dllimport("kernel32.dll", charset = charset.auto, setlasterror = true)]     private static extern intptr getmodulehandle(string lpmodulename);  } 

does have clue why it's hanging after messagebox?

windows cannot dispatch next keyboard message until hook callback finished executing. not want use messagebox.show(), blocks callback. windows puts several seconds before declares code broken , disables hook.

use debug.print() instead.


Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -