2008-12-30 22 views
2

如何在.NET中使用C#抓取类似^ MM(CTRL + M + M)的东西?我怎样才能在.NET中抓取双击键

+0

请提供一些背景。想必你是在谈论WinForms? – 2008-12-30 04:31:27

+0

这在技术上被称为“和弦” – 2008-12-30 06:10:17

+0

谢谢......我不知道名称:) – 2008-12-31 19:36:34

回答

1

这里有一个办法做到这一点:

bool mSeenCtrlM; 

protected override bool ProcessCmdKey(ref Message msg, Keys keyData) { 
    if (keyData == (Keys.Control | Keys.M)) { 
    mSeenCtrlM = !mSeenCtrlM; 
    if (!mSeenCtrlM) { 
     MessageBox.Show("yada"); 
    } 
    return true; 
    } 
    mSeenCtrlM = false; 
    return base.ProcessCmdKey(ref msg, keyData); 
} 
1

这只是一个猜测,您可以在每个按键笔画上存储按键和按键修饰符,然后通过检查为匹配序列按下的最后一个按键。

您可能可以在ProcessCmdKey或OnKeyPress中实现此功能。

1

由另一张海报链接,ModiferKeys是确定是否按下Shift或Control键的方法。另外,如果你重写ProcessCmdKeys这里有一个方法:

private static bool lastKeyWasControlM = false; 

    protected override bool ProcessCmdKey(ref Message msg, Keys keyData) 
    { 
     if (keyData == (Keys.Control | Keys.M)) 
     { 
      lastKeyWasControlM = true; 

      // might want to return true here if Ctrl-M maps to nothing else... 
      // Ideally should start a timer and if the 'M' key press happens 
      // within a short duration (say 1 second) its a combined key event 
      // else its the start of another key event... 
     } 
     else 
     { 
      if ((keyData & Keys.M) == Keys.M && 
       (keyData & Keys.Control) != Keys.Control) 
      { 
       // M pressed with no modifier 
       if (lastKeyWasControlM == true) 
       { 
        // Handle Ctrl-M + M combined key press... 

        return true; 
       } 
      } 

      lastKeyWasControlM = false; 
     } 

     return base.ProcessCmdKey(ref msg, keyData); 
    } 
1

我建议一个更通用的解决方案。做类似的事:

List<Keys> currentKeyStack = new List<Keys>(); 
DateTime lastUpdate = DateTime.Now; 
TimeSpan lengthOfTimeForChordStroke = new TimeSpan(0,0,5); //Whatever you want here. 
protected override bool ProcessCmdKey(Message msg, Keys keyData) 
{ 
    if (DateTime.Now - LastUpdate > lengthOfTimeForChordStroke) 
    { 
      currentKeyStack.Clear(); 
    } 
currentKeyStack.Add(keyData); 

//You now have a list of the the last group of keystrokes that you can process for each key command, for example: 

    if (currentKeyStack.Count == 2) && (currentKeyStack[0] == (Keys.Control | Keys.M)) && (currentKeyStack[1] == (Keys.M)) 
    { 
      MessageBox.Show("W00T!"); 
    } 
} 

该代码可能不是语法正确,但这是一个实现细节。这种事情将会更加扩展,以处理您的关键和弦组合,而不仅仅是一个全部

0

是的,我意识到这有点晚了,但是这个线程帮助了我,所以我尽管我会把它传递回来。

我已经扩展了GWLlosa代码...我也试着慷慨地评论。 这可以让你在代码中构建你的密钥序列。对于Narven,序列会是。

private static List<List<Keys>> command = new List<List<Keys>>{ 
     new List<Keys>{Keys.Control | Keys.M}, 
     new List<Keys>{Keys.M} 
    }; 

 private static List<List<Keys>> command = new List<List<Keys>>{ 
     new List<Keys>{Keys.Control | Keys.M}, 
     new List<Keys>{Keys.Control | Keys.M} 
    }; 

这取决于他正在努力做的事。

下面的完整代码。

// This defines the command sequence. In this case, "ctrl-m, ctrl-m, 1 or 2 or 3 or 4, A" 
    private static List<List<Keys>> command = new List<List<Keys>>{ 
     new List<Keys>{Keys.Control | Keys.M}, 
     new List<Keys>{Keys.Control | Keys.M}, 
     new List<Keys>{Keys.D1, Keys.D2, Keys.D3, Keys.D4 }, 
     new List<Keys>{Keys.A} 
    }; 

    private static List<Keys> currentKeyStack = new List<Keys>(); 
    private static DateTime lastUpdate = DateTime.Now; 

    // See if key pressed within 750ms (0.75 sec) 
    private static TimeSpan lengthOfTimeForChordStroke = new TimeSpan(0, 0, 0, 0, 750); 

    protected static void ProcessCmdKey(Keys keyData) 
    { 
     // Merge Modifiers (Ctrl, Alt, etc.) and key (A, B, 1, 2, etc.) 
     Keys keySequence = (Control.ModifierKeys | keyData); 

     if ((TimeSpan)(DateTime.Now - lastUpdate) > lengthOfTimeForChordStroke) 
     { 
      Console.WriteLine("Clear"); 
      currentKeyStack.Clear(); 
     } 

     int index = currentKeyStack.Count(); 
     Console.WriteLine("Index: " + index); 

     Console.Write("Command: "); 
     foreach (List<Keys> key in command) 
     { 
      foreach (Keys k in key) 
      { 
       Console.Write(" | " + k.ToString() + " (" + (int)k + ")"); 
      } 
     } 
     Console.WriteLine(); 

     Console.Write("Stack: "); 
     foreach (Keys key in currentKeyStack) 
     { 
      Console.Write(" | " + key.ToString() + " (" + (int)key + ")"); 
     } 
     Console.WriteLine(); 

     Console.WriteLine("Diff: " + (TimeSpan)(DateTime.Now - lastUpdate) + " length: " + lengthOfTimeForChordStroke); 
     Console.WriteLine("#: " + index + "KeySeq: " + keySequence + " Int: " + (int)keySequence + " Key: " + keyData + " KeyInt: " + (int)keyData); 

     // .Contains allows variable input, e.g Ctrl-M, Ctrl-M, 1 or 2 or 3 or 4 
     if (command[index].Contains(keySequence)) 
     { 
      Console.WriteLine("Added to Stack!"); 
      currentKeyStack.Add(keySequence); 
     } 
     else 
     { 
      // Clear stack since input didn't match 
      Console.WriteLine("Clear"); 
      currentKeyStack.Clear(); 
     } 

     // When command sequence has been met 
     if (currentKeyStack.Count == command.Count()) 
     { 
      // Do your thing here... 
      Console.WriteLine("CAPTURED: " + currentKeyStack[2]); 
     } 

     // Reset LastUpdate 
     Console.WriteLine("Reset LastUpdate"); 
     lastUpdate = DateTime.Now; 

     Console.WriteLine("\n"); 
    }