我有个问题。我在"KeyPress“上找到的例子,它们不再使用WPF。
您能告诉我,如何只允许在WPF文本框上写入keybord中指定的键?我知道keyUp和Down函数,但是如何定义我想要输入的字母呢?
如果我发布我的代码并告诉你我想做什么,我想这会更容易。这里要换什么?
private void textBox_KeyDown(object sender, KeyEventArgs e)
{
//something here to only allow "A" key to be pressed and displeyed into textbox
if (e.Key == Key.A)
{
stoper.Start();
}
}
private void textBox_KeyUp(object sender, KeyEventArgs e)
{
if (e.Key == Key.A)
{
//here i stop the stopwatch to count time of pressing the key
stoper.Stop();
string aS = stoper.ElapsedMilliseconds.ToString();
int aI = Convert.ToInt32(aS);
stoper.Reset();
}
}
发布于 2018-05-19 17:03:01
您可以使用PreviewKeyDown并使用e.Key
筛选出所需的内容。
或者,在中,您可以在代码的任何位置使用键盘类:
if (Keyboard.IsKeyDown(Key.E)) { /* your code */ }
更新
若要禁止密钥,需要将事件设置为已处理:
if (e.Key == Key.E)
{
e.Handled = true;
MessageBox.Show($"{e.Key.ToString()} is forbidden");
}
发布于 2018-05-20 15:17:08
这个东西对我来说很好(谢谢@JohnyL):
private void textBox_KeyDown(object sender, KeyEventArgs e)
{
//something here to only allow "A" key to be pressed and displeyed into textbox
if (e.Key == Key.A)
{
stoper.Start();
}
else
e.Handled = true;
}
private void textBox_KeyUp(object sender, KeyEventArgs e)
{
if (e.Key == Key.A)
{
//here i stop the stopwatch to count time of pressing the key
stoper.Stop();
string aS = stoper.ElapsedMilliseconds.ToString();
int aI = Convert.ToInt32(aS);
stoper.Reset();
}
}
https://stackoverflow.com/questions/50427556
复制相似问题