Detecting input keystroke during WPF processing -


greetings,

i want write code executes within event handler, inside wpf windows application, can detect keypress, "escape" character keypress, within processing loop. allow user escape processing. realize may accomplished kind of multi-threaded approach, problem seems simple wondered if might accomplished follows:

// attempt 1: see if keyboard static iskeydown method detects key presses while executing.
// note not successful. keyboard states not appear updated during processing.

        bool iskeypressed = false;         while (!iskeypressed)         {             system.threading.thread.sleep(1000);             if (keyboard.iskeydown(key.enter))                 iskeypressed = true;           } 

so, on attempt #2. saw articles , samples using pinvoke "getkeyboardstate" method. i'm not sure used method correctly, here attempt. bit clumsy refer windows.forms enumeration in wpf application, seems work.

// attempt 2: use pinvoke getkeyboardstate method.
// far, i've been unsuccessful well, i'm not sure usage correct.

        bool iskeypressed = false;         while (!iskeypressed)         {             system.threading.thread.sleep(1000);             if (isescapepressed())                  iskeypressed = true;           }     }       [dllimport("user32.dll")] public static extern int getkeyboardstate(byte[] lpkeystate);     private bool isescapepressed()     {         byte[] keyboardstate = new byte[255];         int keystate = getkeyboardstate(keyboardstate);          if (keyboardstate[(int)system.windows.forms.keys.escape] == 128)             return true;         else             return false;      }     

but unfortunately, i'm not seeing change in keyboard states executes. played around little calls dispatcher see if keyboard information refresh during processing, have not been successful technique.

i'm out of ideas. can propose something? thank in advance assistance.

  • david

something this:

private bool iscancelled { get; set; }  private void onbuttonclick(object sender, eventargs e) {    action doworkdelegate = dowork;     doworkdelegate.begininvoke(null, null); }  protected override void onkeydown(keyeventargs e) {     if (e.key == key.escape) {         iscancelled = true;         e.handled = true;     } else {         base.onkeydown(e);     } }  private void dowork() {    iscancelled  = false;    while (!iscancelled)    {        system.threading.thread.sleep(1000);    } } 

the important point method work executed in separate thread main thread can process user input (key strokes).


Comments

Popular posts from this blog

java - SNMP4J General Variable Binding Error -

windows - Python Service Installation - "Could not find PythonClass entry" -

Determine if a XmlNode is empty or null in C#? -