在我的 C# 项目中,我使用 Timer.One(keyboard_timer) 用于观察用户是否按下 F8,另一个 Timer(clipboard_time) 用于观察剪贴板是否包含文本。在我的项目中,键盘始终启用当用户按下 F8 时,clipboard_timer 被启用。如果用户再次按下 F8,clipboard_timer 被禁用。我的项目做了什么当用户按下 F8 并复制一个单词时,我的项目会在窗口中显示复制的单词的含义。我的程序在后台运行并始终检查用户是否按 F8,如果他这样做了,那么我的程序一直检查剪贴板,如果它包含文本(单词),如果它每次都显示单词的含义。我的代码在这里:
在初始化
keyboard_timer.Enabled = true;
keyboard_timer.Tick += new EventHandler(keyboard_timer_Tick);
然后
public void keyboard_timer_Tick(object sender, EventArgs e)
{
// clipboard enable
if ((a % 2) != 0)
{
// F9 is for Easy mood to eanble
if ((GetAsyncKeyState(Keys.F9) == -32767) && hot_key == "F9")
{
label2.Text = "Easy";
online_clipboard_active = "";
Clipboard.Clear();
clipboard_timer.Enabled = true;
clipboard_timer.Tick += new EventHandler(clipboard_timer_Tick);
++a;
}
///// // F8 is for online Mood
else if ((GetAsyncKeyState(Keys.F8) == -32767) && online_hot_key == "F8")
{
label2.Text = "Online";
online_clipboard_active = "on";
clipboard_timer.Enabled = true;
clipboard_timer.Tick += new EventHandler(clipboard_timer_Tick);
++a;
}
}// end of enable
//clipboard disable
if ((a % 2) == 0) //
{
// F9 is for Easy mood to disable here
if ((GetAsyncKeyState(Keys.F9) == -32767) && hot_key == "F9")
{
label2.Text = "Off";
clipboard_timer.Enabled = false;
++a;
}
// F8 is for online Mood to disable here
else if ((GetAsyncKeyState(Keys.F8) == -32767) && online_hot_key == "F8")
{
label2.Text = "Off";
online_clipboard_active = "";
clipboard_timer.Enabled = false;
++a;
}
}//end of clipboard disable
}// end of keyboard timer
剪贴板计时器是
public void clipboard_timer_Tick(object sender, EventArgs e)
{
if (Clipboard.GetDataObject().GetDataPresent(DataFormats.Text))
{
string x = Clipboard.GetText();
Clipboard.Clear();
if ((a%2)==0 && online_clipboard_active == "on")
{
//cal online_mood form to translate the string from googletranslator
online_mood o = new online_mood(x);
o.Show();
}
else if((a%2)==0 && online_clipboard_active == "")
{
//cal show_meaning form to show the meaning into a window
show_meaning s = new show_meaning(x);
s.Show();
}
}
}// end of clipboard timer Tick
我希望当剪贴板定时器启用时键盘定时器将被锁定,因为它们都使用变量。当剪贴板计时器运行时,键盘计时器将被锁定,然后当剪贴板计时器完成时它会工作,然后键盘计时器将被重新激活我该如何解决这个问题?有人帮帮我吗??????