我正在使用Code Project中的这篇文章:
然后我在那里尝试某人回应了这篇文章并提供了一个解决方案,例如按键组合键:左Ctrl + G. 所以他为此增加了一个新类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Utilities;
namespace Utilities
{
class Combinaison
{
public delegate void EventFunction(List<Keys> keys);
private globalKeyboardHook _gkh = new globalKeyboardHook();
private EventFunction _f;
private List<Keys> _combinaison;
private List<bool> _pressed = new List<bool>();
private bool _event = false;
public Combinaison(List<Keys> combinaison, EventFunction f)
{
_f = f;
_combinaison = combinaison;
foreach (Keys k in _combinaison)
{
_gkh.HookedKeys.Add(k);
_pressed.Add(false);
}
_gkh.KeyDown += new KeyEventHandler(gkh_KeyDown);
_gkh.KeyUp += new KeyEventHandler(gkh_KeyUp);
}
private void gkh_KeyUp(object sender, KeyEventArgs e)
{
e.Handled = true;
_event = false;
_pressed[_combinaison.IndexOf(e.KeyCode)] = false;
}
private void gkh_KeyDown(object sender, KeyEventArgs e)
{
e.Handled = true;
if (_combinaison.IndexOf(e.KeyCode) == _combinaison.Count - 1)
{
bool before_pressed = true;
for (int i = 0; i < _combinaison.IndexOf(e.KeyCode); i++)
{
if (_pressed[i] == false)
{
before_pressed = false;
}
}
if (before_pressed == true && _event == false)
{
_f(_combinaison);
_event = true;
}
}
else
{
_pressed[_combinaison.IndexOf(e.KeyCode)] = true;
}
}
}
}
然后我在form1中使用它,就像我添加到form1 top:
private globalKeyboardHook gkh = new globalKeyboardHook();
然后在form1加载事件中我做了:
List<Keys> C1 = new List<Keys> { Keys.LControlKey, Keys.G };
Combinaison Combinaison = new Combinaison(C1, Console);
然后是Console方法:
public void Console(List<Keys> keys)
{
clickNo ++;
start = DateTime.Now;
progressBar1.Maximum = 1;
progressBar1.Step = 1;
progressBar1.Value = 0;
switch (clickNo)
{
case 1:
DoRequest(ScreenshotRequest.DannysCommands.NormalOperation); break;
case 2:
DoRequest(ScreenshotRequest.DannysCommands.Displayoverlays); break;
case 3:
DoRequest(ScreenshotRequest.DannysCommands.Dontdisplayoverlays); break;
}
}
clickNo是交换机的全局int变量,使用案例。
这里的问题是:
如果键的组合是左Ctrl + O,则我不能在程序运行字母O时输入textBox,或者如果它是G,则我不能为某些键入G这个事件锁定键的原因是它可以用左Ctrl键按下togeather但我不能用它作为单个字母,例如,如果我想输入一个textBox。
如何修复此组合课程事件?