我一直在插入像'xx - xx - xx \ username'这样的文字。所以在按 Alt / Ctrl + 另一个键后,我需要输入一些文字。
在仔细阅读了这个问题后,我发现了一些有用的信息:
How to detect the currently pressed key?
我有问题要考虑如何做到这一点。 任何其他人都应该像他需要的那样定义自己的'xx - xx - xx \ username'。因此,将其保存到文件中是下一步。
Stack Overflow上有关于此问题的任何线程吗?我找不到任何结果,也许我错了。
答案 0 :(得分:1)
要捕获用于检测用户是否按下了键盘快捷方式的KeyDown
事件,请使用此代码处理事件KeyDown
(其中txtInput
为TextBox
)
this.txtInput.KeyDown += new System.Windows.Forms.KeyEventHandler(this.txtInput_KeyDown);
现在创建上述方法(txtInput_KeyDown
)并添加以下代码以处理特定的快捷方式
(P.S.处理事件并在VisualStudio中创建方法,即使在属性中也只需双击KeyDown
)
// Shortcut Alt+I
if (e.Alt && (e.KeyCode == Keys.I))
{
txtInput.Text = @"somedomain\" + txtInput.Text; // Adds the text to the front of the current text
txtInput.SelectionStart = txtInput.Text.Length; // Sets the cursor to the end of the text
}
// Shortcut Ctrl+K
else if (e.Control && (e.KeyCode == Keys.K))
{
txtInput.Text += @"networkpath\"; // Adds the text to the back of the current text
txtInput.SelectionStart = txtInput.Text.Length; // Sets the cursor to the end of the text
}
如代码中的注释所述,此代码将以不同方式处理两种不同的快捷方式组合,并根据您的需要进行修改。
关于KeyDown事件可以进行额外的阅读here。
您提到的下一步是保存信息并在应用程序再次启动时加载它,阅读有关存储和检索用户设置的SO帖子here和here。
有关用户设置的MSDN条目:
Using Application Settings and User Settings
How To: Write User Settings at Run Time with C#
Using Settings in C#