通过键盘显示和隐藏窗体 C#
show and hide form by keyboard c#
我有一个表单,我想在按 F1 时显示和隐藏我的表单,我尝试使用 Global Low Level Keyboard Hook 按照本教程进行操作:
https://www.codeproject.com/articles/19004/a-simple-c-global-low-level-keyboard-hook
我按 1x 表单隐藏,如果我再次按显示表单不久之后它不起作用。
namespace MyForm
{
public partial class Form1 : MetroForm
{
public Form1()
{
InitializeComponent();
}
globalKeyboardHook gkh = new globalKeyboardHook();
private void Form1_Load(object sender, EventArgs e)
{
gkh.HookedKeys.Add(Keys.F1);
gkh.KeyDown += new KeyEventHandler(gkh_KeyDown);
gkh.KeyUp += new KeyEventHandler(gkh_KeyUp);
}
void gkh_KeyUp(object sender, KeyEventArgs e)
{
this.Show();
e.Handled = true;
}
void gkh_KeyDown(object sender, KeyEventArgs e)
{
this.Hide();
e.Handled = true;
}
private void Form1_Closing(object sender, EventArgs e)
{
gkh.unhook();
}
}
}
感谢任何帮助
你正在做的是同时按下按钮和释放按钮,这样它会同时触发两个事件而不是像这样做
public static bool flag = true;
public Form1()
{
InitializeComponent();
}
globalKeyboardHook gkh = new globalKeyboardHook();
private void Form1_Load(object sender, EventArgs e)
{
gkh.HookedKeys.Add(Keys.F1);
gkh.KeyDown += new KeyEventHandler(gkh_KeyDown);
}
void gkh_KeyDown(object sender, KeyEventArgs e)
{
if (flag)
{
this.Hide();
flag = false;
}
else
{
this.Show();
flag = true;
}
e.Handled = true;
}
private void Form1_Closing(object sender, EventArgs e)
{
gkh.unhook();
}
你必须在一个事件中处理它,这将是 keydown
因为你正在按 f1
而且我还添加了标志来检查表单是否隐藏它会显示,如果表单显示隐藏它
我有一个表单,我想在按 F1 时显示和隐藏我的表单,我尝试使用 Global Low Level Keyboard Hook 按照本教程进行操作: https://www.codeproject.com/articles/19004/a-simple-c-global-low-level-keyboard-hook
我按 1x 表单隐藏,如果我再次按显示表单不久之后它不起作用。
namespace MyForm
{
public partial class Form1 : MetroForm
{
public Form1()
{
InitializeComponent();
}
globalKeyboardHook gkh = new globalKeyboardHook();
private void Form1_Load(object sender, EventArgs e)
{
gkh.HookedKeys.Add(Keys.F1);
gkh.KeyDown += new KeyEventHandler(gkh_KeyDown);
gkh.KeyUp += new KeyEventHandler(gkh_KeyUp);
}
void gkh_KeyUp(object sender, KeyEventArgs e)
{
this.Show();
e.Handled = true;
}
void gkh_KeyDown(object sender, KeyEventArgs e)
{
this.Hide();
e.Handled = true;
}
private void Form1_Closing(object sender, EventArgs e)
{
gkh.unhook();
}
}
}
感谢任何帮助
你正在做的是同时按下按钮和释放按钮,这样它会同时触发两个事件而不是像这样做
public static bool flag = true;
public Form1()
{
InitializeComponent();
}
globalKeyboardHook gkh = new globalKeyboardHook();
private void Form1_Load(object sender, EventArgs e)
{
gkh.HookedKeys.Add(Keys.F1);
gkh.KeyDown += new KeyEventHandler(gkh_KeyDown);
}
void gkh_KeyDown(object sender, KeyEventArgs e)
{
if (flag)
{
this.Hide();
flag = false;
}
else
{
this.Show();
flag = true;
}
e.Handled = true;
}
private void Form1_Closing(object sender, EventArgs e)
{
gkh.unhook();
}
你必须在一个事件中处理它,这将是 keydown
因为你正在按 f1
而且我还添加了标志来检查表单是否隐藏它会显示,如果表单显示隐藏它