我想检查C#Windows窗体应用程序中的Insert键的状态.这是最小的代码(不起作用;带有两个RadioButtons的表单):
using System;
using System.Windows.Forms;
using System.Windows.Input;
// Also added PresentationCore and WindowsBase refereneces
namespace InsertModeDemo1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
if (Keyboard.IsKeyToggled(Key.Insert))
radioButtonInsert.Checked = true;
else
radioButtonOverstrike.Checked = true;
}
}
}
Run Code Online (Sandbox Code Playgroud)
请尝试使用Control.IsKeyLocked.
private void Form1_Load(object sender, EventArgs e)
{
if (Control.IsKeyLocked(Keys.Insert))
radioButtonInsert.Checked = true;
else
radioButtonOverstrike.Checked = true;
}
Run Code Online (Sandbox Code Playgroud)
参考文献:Control.IsKeyLocked
注意
该文件说,该方法只作品有CAPS LOCK,NUM LOCK或SCROLL LOCK唯一的关键.但测试该方法
Keys.Insert已经证明它也适用于INSERT密钥.