1
private void timer1_Tick(object sender, EventArgs e){

if (Form.ModifierKeys == System.Windows.Forms.Keys.Control
 && Form.ModifierKeys == System.Windows.Forms.Keys.Enter)

my_translate(textbox1.text); 

}

.

I try it but dont work how can I do it?

I am writing a dictionary software; with timer I check determine pressed keys so I translate word. I cant use textBox1_KeyPress etc. because I get text from .doc/.txt so I need timer for get text.

//The code is working
private void timer1_Tick(object sender, EventArgs e){ 
    MouseButtons aa = MouseButtons;
    if (aa == MouseButtons.Middle && Form.ModifierKeys == Keys.Control)

            my_translate();
} 

.

And We have a alnernative for timer to call a method when user pressed a key combination?

4

1 回答 1

1

您当前的代码

if (Form.ModifierKeys == System.Windows.Forms.Keys.Control && 
    Form.ModifierKeys == System.Windows.Forms.Keys.Enter)

意思是“如果按下的键等于控制键并且按下的键等于回车键”。这永远不会发生,因为如果只按下控制键,则不按下回车键,反之亦然。

我相信你想要这个:

if (Form.ModifierKeys.HasFlag(Keys.Control) && 
    Form.ModifierKeys.HasFlag(Keys.Enter))

这意味着“如果按下的键包括控制键和回车键”。

无论如何,您不应该为此使用计时器。查看 Control.KeyPress 事件并改用它。您可以使用计时器来加载文本文件,同时使用事件来处理按键。

我建议您在 MSDN 上阅读有关 KeyPress 事件的更多信息:http: //msdn.microsoft.com/en-us/library/system.windows.forms.control.keypress.aspx

于 2013-06-23T01:48:41.860 回答