如何检测当前按下的键?在……里面Windows窗体,您可以随时知道光标的当前位置,这要归功于游标班级,等级。同样的东西似乎不适用于键盘。是否有可能知道,例如,换档按下键了吗?是否绝对有必要追踪每一个键盘通知(按键和按键事件)?
3 回答
缥缈止盈
TA贡献2041条经验 获得超4个赞
if ((Control.ModifierKeys & Keys.Shift) != 0)
if (Control.ModifierKeys == Keys.Shift)
Control
Control.
猛跑小猪
TA贡献1858条经验 获得超8个赞
private KeyMessageFilter m_filter = new KeyMessageFilter();private void Form1_Load(object sender, EventArgs e){ Application.AddMessageFilter(m_filter);}public class KeyMessageFilter : IMessageFilter{ private const int WM_KEYDOWN = 0x0100; private const int WM_KEYUP = 0x0101; private bool m_keyPressed = false; private Dictionary<Keys, bool> m_keyTable = new Dictionary<Keys, bool>(); public Dictionary<Keys, bool> KeyTable { get { return m_keyTable; } private set { m_keyTable = value; } } public bool IsKeyPressed() { return m_keyPressed; } public bool IsKeyPressed(Keys k) { bool pressed = false; if (KeyTable.TryGetValue(k, out pressed)) { return pressed; } return false; } public bool PreFilterMessage(ref Message m) { if (m.Msg == WM_KEYDOWN) { KeyTable[(Keys)m.WParam] = true; m_keyPressed = true; } if (m.Msg == WM_KEYUP) { KeyTable[(Keys)m.WParam] = false; m_keyPressed = false; } return false; }}
- 3 回答
- 0 关注
- 724 浏览
添加回答
举报
0/150
提交
取消