我想要做的是實現一個TextBox
,它接受一個組合鍵作爲輸入。例如,文本Win + Ctrl + A
在按下這些鍵時出現(Windows窗體或WPF)。實現快捷方式編輯器TextBox的最佳方式是什麼?
一個很好的例子是在Winamp設置下的「Global Hotkeys」下。
我想要做的是實現一個TextBox
,它接受一個組合鍵作爲輸入。例如,文本Win + Ctrl + A
在按下這些鍵時出現(Windows窗體或WPF)。實現快捷方式編輯器TextBox的最佳方式是什麼?
一個很好的例子是在Winamp設置下的「Global Hotkeys」下。
這是你如何可以檢測幾乎是被完全按下的鍵: 來源:How to detect the currently pressed key?
private KeyMessageFilter m_filter = new KeyMessageFilter();
private void Form1_Load(object sender, EventArgs e)
{
Application.AddMessageFilter(m_filter);
}
public class KeyMessageFilter : IMessageFilter
{
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;
}
private const int WM_KEYDOWN = 0x0100;
private const int WM_KEYUP = 0x0101;
private bool m_keyPressed = 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;
}
}
有趣的是,我可能會試試這個。 – Tibi 2012-02-23 17:53:39
檢查了這一點:
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
HotKeyCls h = new HotKeyCls() { Ctrl = e.Control, Alt = e.Alt, Shift = e.Shift, Key = e.KeyCode };
textBox1.Text = h.ToString();
}
HotKeyCls:
//Coded by Amen Ayach's DataClassBuilder @23/02/2012
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
public class HotKeyCls{
private bool _Ctrl;
public bool Ctrl{
get {
return _Ctrl;
}
set {
_Ctrl = value;
}
}
private bool _Shift;
public bool Shift{
get {
return _Shift;
}
set {
_Shift = value;
}
}
private bool _Alt;
public bool Alt{
get {
return _Alt;
}
set {
_Alt = value;
}
}
private System.Windows.Forms.Keys _Key;
public System.Windows.Forms.Keys Key
{
get {
return _Key;
}
set {
_Key = value;
}
}
public override string ToString()
{
return (Ctrl ? "Ctrl+" : "") + (Shift ? "Shift+" : "") + (Alt ? "Alt+" : "") + Key.ToString();
}
}
@DJKRAZE我想他想讓文本框向他展示被按下的內容。不發送印刷機。 – DaveShaw 2012-02-23 17:44:32
@DaveShaw準確 – Tibi 2012-02-23 17:49:47
他可以做到這一點以及通過捕獲按鍵,如果有按鍵的3個組合鍵他按代碼得到在if(e.KeyCode == Keys.Enter && Key.CTRL && key.H)for例如在KeyDownEvent上做這件事..這應該有助於你開始..如果(e.KeyCode == Win + Crl + A)追加密鑰。對於Win,Ctrl,A – MethodMan 2012-02-23 17:50:22