c# - Thread to delay a process when F4 key detected -


please refer following code

static class program {      [flags]     private enum keystates     {         none = 0,         down = 1,         toggled = 2     }      [dllimport("user32.dll", charset = charset.auto, exactspelling = true)]     private static extern short getkeystate(int keycode);      private static keystates getkeystate(keys key)     {         keystates state = keystates.none;          short retval = getkeystate((int)key);          //if high-order bit 1, key down         //otherwise, up.         if ((retval & 0x8000) == 0x8000)             state |= keystates.down;          //if low-order bit 1, key toggled.         if ((retval & 1) == 1)             state |= keystates.toggled;          return state;     }      public static bool iskeydown(keys key)     {         return keystates.down == (getkeystate(key) & keystates.down);     }      public static bool iskeytoggled(keys key)     {         return keystates.toggled == (getkeystate(key) & keystates.toggled);     }       private static void dosomething(object state)     {           if (iskeydown(keys.f4))            {               system.threading.thread.sleep(30000);           }     }      [stathread]      static void main()     {          console.writeline("application started");         system.threading.timer mytimer = new system.threading.timer(new timercallback(dosomething), null, 0, 1000);         // execute long codes here     } } 

whenever system detect "f4" keypressed event, delay 30 seconds .. current code, seems process never delayed though user pressed key f4...

thanks.

getkeystate() can work in gui app. provides synchronous state of key, recorded @ time key press detected , added message queue. important ensure state of modifier keys can detected example, might have changed time keystroke processed.

this console mode app, must use getasynckeystate() instead.


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -