请问在C#的textbox空间里 如何实现 输入按键 显示按键名 70

就是比如在一个游戏里设置一个快捷键窗体上一个label空间显示快捷功能名称后面有个textbox用于用户输入指定按键,输入键以后,显示这个键的名称,输入f就显示F,输入F... 就是比如在一个游戏里设置一个快捷键
窗体上一个label空间显示快捷功能名称 后面有个textbox用于用户输入指定按键,输入键以后,显示这个键的名称,输入f就显示F,输入F1就显示F1 然后再用button的click方法把输入的键值写入XML文件.
现在遇到的问题是 我用一些普通的方法 比如在textbox的keypress方法里,用e.keychar.tostring()这样的方法,无法捕获F1-F9以及DELETE这样的功能键,以及CTRL,ALT,SHIFT+普通键 这样的符合键.
查阅MSDN看到ConsoleKeyInfo可以在控制台命令程序里很方便实现上面我说的功能,但是在窗体控件里,我用同样的方法却显示失败.
请教教我这个功能怎么实现.读写XML我会了,就是怎么输入按键显示 按键名这个功能
已经用keyup事件实现
代码如
private void textBox1_KeyUp(object sender, KeyEventArgs e)
{
textBox1.Text = e.KeyCode.ToString();
e.Handled = true;
}
但是还是有问题 如果是ctrl alt shift+字母键这样的组合 就无法了.继续求救
展开
 我来答
百度网友0ff5a102c
2008-06-09
知道答主
回答量:0
采纳率:0%
帮助的人:0
展开全部
这是一个快捷键的控件,把它放到自己的程序集里,就能像普通控件一样使用。

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace System.Windows.Forms.HotKeyTextBox
{

class HotKey
{
private WinHotKey.KeyModifiers _Modifiers;
private Keys _Key;
public WinHotKey.KeyModifiers Modifiers
{
set { _Modifiers = value; } //& WinHotKey.KeyModifiers.Mask; }
get { return _Modifiers; }
}
public Keys Key
{
set { _Key = value; }
get { return _Key; }
}
}

class WinHotKey
{
private IntPtr Handle;
private int Count = 0;
/// <summary>
/// 初始化
/// </summary>
/// <param name="Handle">窗口的句柄</param>
public WinHotKey(IntPtr Handle)
{
this.Handle = Handle;
}
/// <summary>
/// 自动清除快捷键
/// </summary>
~WinHotKey()
{
for (int i = 0; i < Count; i++)
UnregisterHotKey(Handle, i);
}
/// <summary>
/// 设置一个快捷键
/// </summary>
/// <param name="HotKey">快捷键列表</param>
public void SetHotKey(params HotKey[] @HotKey)
{
Count = @HotKey.Length;
for (int i = 0; i < Count; i++)
RegisterHotKey(Handle, i, @HotKey[i].Modifiers, @HotKey[i].Key);
}

[DllImport("user32.dll", SetLastError = true)]
public static extern bool RegisterHotKey(IntPtr hWnd, int id, KeyModifiers fsModifiers, Keys vk);

[DllImport("user32.dll", SetLastError = true)]
public static extern bool UnregisterHotKey(IntPtr hWnd, int id);

//[Flags()]
public enum KeyModifiers
{
None = 0,
Alt = 1,
Control = 2,
Shift = 4,
Windows = 8,
// Mask = 0xf
}
}

class HotKeyTextBox : TextBox
{
private HotKey _HotKey = new HotKey();
private Keys LastKey;

public HotKeyTextBox()
{
this.KeyDown += new KeyEventHandler(HotKeyTextBox_KeyDown);
this.KeyUp += new KeyEventHandler(HotKeyTextBox_KeyUp);
this.KeyPress += new KeyPressEventHandler(HotKeyTextBox_KeyPress);
}

private void HotKeyTextBox_KeyPress(object sender, KeyPressEventArgs e)
{
e.Handled = true;
}

private void HotKeyTextBox_KeyUp(object sender, KeyEventArgs e)
{
if (!(e.KeyValue == 18 | e.KeyValue == 17 | e.KeyValue == 16)) LastKey = Keys.None;
if (_HotKey.Key == Keys.None)
{
string v = string.Empty;
_HotKey.Modifiers = WinHotKey.KeyModifiers.None;
if (e.Control)
{
_HotKey.Modifiers |= WinHotKey.KeyModifiers.Control;
v += "Ctrl + ";
}
if (e.Shift)
{
_HotKey.Modifiers |= WinHotKey.KeyModifiers.Shift;
v += "Shift + ";
}
if (e.Alt)
{
_HotKey.Modifiers |= WinHotKey.KeyModifiers.Alt;
v += "Alt + ";
}
if (string.IsNullOrEmpty(v)) this.Text = Keys.None.ToString();
else this.Text = v;
}
}

private void HotKeyTextBox_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyValue == 46)
{
_HotKey.Key = Keys.None;
_HotKey.Modifiers = WinHotKey.KeyModifiers.None;
this.Text = string.Empty;
}
string v = string.Empty;
_HotKey.Modifiers = WinHotKey.KeyModifiers.None;
if (e.Control)
{
_HotKey.Modifiers |= WinHotKey.KeyModifiers.Control;
v += "Ctrl + ";
}
if (e.Shift)
{
_HotKey.Modifiers |= WinHotKey.KeyModifiers.Shift;
v += "Shift + ";
}
if (e.Alt)
{
_HotKey.Modifiers |= WinHotKey.KeyModifiers.Alt;
v += "Alt + ";
}
if (e.KeyValue == 18 | e.KeyValue == 17 | e.KeyValue == 16)
{
if (LastKey == Keys.None)
_HotKey.Key = Keys.None;
else
v += _HotKey.Key.ToString();
}
else
{
_HotKey.Key = e.KeyCode;
v += _HotKey.Key.ToString();
}
LastKey = _HotKey.Key;
this.Text = v;
}

public HotKey @HotKey
{
set
{
_HotKey = value;
string v = string.Empty;
if ((value.Modifiers & WinHotKey.KeyModifiers.Control) == WinHotKey.KeyModifiers.Control) v += "Ctrl + ";
if ((value.Modifiers & WinHotKey.KeyModifiers.Shift) == WinHotKey.KeyModifiers.Shift) v += "Shift + ";
if ((value.Modifiers & WinHotKey.KeyModifiers.Alt) == WinHotKey.KeyModifiers.Alt) v += "Alt + ";
v += _HotKey.Key.ToString();
this.Text = v;
}
get
{
return _HotKey;
}
}
}
}
硕照回文昌
2020-06-16
知道答主
回答量:0
采纳率:0%
帮助的人:0
展开全部
这是一个快捷键的控件,把它放到自己的程序集里,就能像普通控件一样使用。
using
System;
using
System.Collections.Generic;
using
System.Text;
using
System.Windows.Forms;
namespace
System.Windows.Forms.HotKeyTextBox
{
class
HotKey
{
private
WinHotKey.KeyModifiers
_Modifiers;
private
Keys
_Key;
public
WinHotKey.KeyModifiers
Modifiers
{
set
{
_Modifiers
=
value;
}
//&
WinHotKey.KeyModifiers.Mask;
}
get
{
return
_Modifiers;
}
}
public
Keys
Key
{
set
{
_Key
=
value;
}
get
{
return
_Key;
}
}
}
class
WinHotKey
{
private
IntPtr
Handle;
private
int
Count
=
0;
///
<summary>
///
初始化
///
</summary>
///
<param
name="Handle">窗口的句柄</param>
public
WinHotKey(IntPtr
Handle)
{
this.Handle
=
Handle;
}
///
<summary>
///
自动清除快捷键
///
</summary>
~WinHotKey()
{
for
(int
i
=
0;
i
<
Count;
i++)
UnregisterHotKey(Handle,
i);
}
///
<summary>
///
设置一个快捷键
///
</summary>
///
<param
name="HotKey">快捷键列表</param>
public
void
SetHotKey(params
HotKey[]
@HotKey)
{
Count
=
@HotKey.Length;
for
(int
i
=
0;
i
<
Count;
i++)
RegisterHotKey(Handle,
i,
@HotKey[i].Modifiers,
@HotKey[i].Key);
}
[DllImport("user32.dll",
SetLastError
=
true)]
public
static
extern
bool
RegisterHotKey(IntPtr
hWnd,
int
id,
KeyModifiers
fsModifiers,
Keys
vk);
[DllImport("user32.dll",
SetLastError
=
true)]
public
static
extern
bool
UnregisterHotKey(IntPtr
hWnd,
int
id);
//[Flags()]
public
enum
KeyModifiers
{
None
=
0,
Alt
=
1,
Control
=
2,
Shift
=
4,
Windows
=
8,
//
Mask
=
0xf
}
}
class
HotKeyTextBox
:
TextBox
{
private
HotKey
_HotKey
=
new
HotKey();
private
Keys
LastKey;
public
HotKeyTextBox()
{
this.KeyDown
+=
new
KeyEventHandler(HotKeyTextBox_KeyDown);
this.KeyUp
+=
new
KeyEventHandler(HotKeyTextBox_KeyUp);
this.KeyPress
+=
new
KeyPressEventHandler(HotKeyTextBox_KeyPress);
}
private
void
HotKeyTextBox_KeyPress(object
sender,
KeyPressEventArgs
e)
{
e.Handled
=
true;
}
private
void
HotKeyTextBox_KeyUp(object
sender,
KeyEventArgs
e)
{
if
(!(e.KeyValue
==
18
|
e.KeyValue
==
17
|
e.KeyValue
==
16))
LastKey
=
Keys.None;
if
(_HotKey.Key
==
Keys.None)
{
string
v
=
string.Empty;
_HotKey.Modifiers
=
WinHotKey.KeyModifiers.None;
if
(e.Control)
{
_HotKey.Modifiers
|=
WinHotKey.KeyModifiers.Control;
v
+=
"Ctrl
+
";
}
if
(e.Shift)
{
_HotKey.Modifiers
|=
WinHotKey.KeyModifiers.Shift;
v
+=
"Shift
+
";
}
if
(e.Alt)
{
_HotKey.Modifiers
|=
WinHotKey.KeyModifiers.Alt;
v
+=
"Alt
+
";
}
if
(string.IsNullOrEmpty(v))
this.Text
=
Keys.None.ToString();
else
this.Text
=
v;
}
}
private
void
HotKeyTextBox_KeyDown(object
sender,
KeyEventArgs
e)
{
if
(e.KeyValue
==
46)
{
_HotKey.Key
=
Keys.None;
_HotKey.Modifiers
=
WinHotKey.KeyModifiers.None;
this.Text
=
string.Empty;
}
string
v
=
string.Empty;
_HotKey.Modifiers
=
WinHotKey.KeyModifiers.None;
if
(e.Control)
{
_HotKey.Modifiers
|=
WinHotKey.KeyModifiers.Control;
v
+=
"Ctrl
+
";
}
if
(e.Shift)
{
_HotKey.Modifiers
|=
WinHotKey.KeyModifiers.Shift;
v
+=
"Shift
+
";
}
if
(e.Alt)
{
_HotKey.Modifiers
|=
WinHotKey.KeyModifiers.Alt;
v
+=
"Alt
+
";
}
if
(e.KeyValue
==
18
|
e.KeyValue
==
17
|
e.KeyValue
==
16)
{
if
(LastKey
==
Keys.None)
_HotKey.Key
=
Keys.None;
else
v
+=
_HotKey.Key.ToString();
}
else
{
_HotKey.Key
=
e.KeyCode;
v
+=
_HotKey.Key.ToString();
}
LastKey
=
_HotKey.Key;
this.Text
=
v;
}
public
HotKey
@HotKey
{
set
{
_HotKey
=
value;
string
v
=
string.Empty;
if
((value.Modifiers
&
WinHotKey.KeyModifiers.Control)
==
WinHotKey.KeyModifiers.Control)
v
+=
"Ctrl
+
";
if
((value.Modifiers
&
WinHotKey.KeyModifiers.Shift)
==
WinHotKey.KeyModifiers.Shift)
v
+=
"Shift
+
";
if
((value.Modifiers
&
WinHotKey.KeyModifiers.Alt)
==
WinHotKey.KeyModifiers.Alt)
v
+=
"Alt
+
";
v
+=
_HotKey.Key.ToString();
this.Text
=
v;
}
get
{
return
_HotKey;
}
}
}
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
wozuiruhe
2008-06-09
知道答主
回答量:0
采纳率:0%
帮助的人:0
展开全部
CTRl+普通键的方法:按下ctrl+普通键后,e.keychar会得到值,只是不一定能够正常显示,譬如ctrl+a得到的char对应的整数是1,ctrl+b得到的char对应的整数是2,你自己建立一个对应关系,得到1显示ctrl+a,得到2显示ctrl+b就可以了。
textbox的keypress貌似不响应功能键的按下……等待高手
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
百度网友96eea501b
2008-06-11
知道答主
回答量:0
采纳率:0%
帮助的人:0
展开全部
按键的集合不一样
e.KeyCode == Keys.F4
e.Modifiers == Keys.Alt

这样应该明白了吧。
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
匿名用户
2008-06-09
展开全部
用哈希表
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(3)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式