如何使用KeyeventHandler捕获空格键的按下事件? [英] How to capture the spacebar press event using KeyeventHandler?
问题描述
我有一个带有富文本框的表单,我想在其中执行以下操作:
I have a Form with a rich text box in which i want to do the following:
当用户按下空格键时(当前我正在执行keydown事件,但想使用按键事件,但不提供e.keycode),应调用一个函数,在该函数中应实现以下逻辑:
When user presses the spacebar button (Currently i am doing it with keydown event but want to use key press event but it doesn't provide e.keycode), a function should be called in which this logic is to be implemented:
要提取最后一个单词并在富文本框的文本中循环,以便在富文本框中找到其出现的次数.
last written word is to be fetched and is to be looped through the text of rich text box in order to find its number of occurrences in a rich text box.
到目前为止,我所做的是:
What i have done so far is:
private void textContainer_rtb_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Space)
{
String abc = this.textContainer_rtb.Text.Split(' ').Last();
chkWordRepeat(abc);
}
}
public void chkWordRepeat(String lastWordToFind)
{
int count = new Regex(lastWordToFind).Matches(this.textContainer_rtb.Text.Split(' ').ToString()).Count;
MessageBox.Show("Word: " + lastWordToFind + "has come: " + count + "times");
}
请让我知道上面提到的逻辑是否正确,如何将这个逻辑与空格键的按下事件关联起来?如果没有,请帮助我实施!
Please let me know if the above mentioned logic is correct or not And how can i attach this logic with key press event for spacebar? If not then please help me implementing!
谢谢.
推荐答案
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == ' ')
MessageBox.Show("space pressed");
}
这篇关于如何使用KeyeventHandler捕获空格键的按下事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!