如何使用C#在.NET中抓取^ MM(CTRL + M + M)之类的内容?
答案 0 :(得分:1)
这只是一个猜测,您可以在每个键击上存储键和键修饰符,然后下一次检查按下的最后一个键是否匹配序列。
您可以在ProcessCmdKey或OnKeyPress中实现此功能。
答案 1 :(得分: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);
}
答案 2 :(得分: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);
}
答案 3 :(得分: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!");
}
}
代码可能在语法上不正确,但这是一个实现细节。对于处理所有你的关键和弦组合,这种事情会更容易扩展,而不只是一个。
答案 4 :(得分: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");
}