[英]Choosing & saving your own hotkeys on multiple forms
目前我有两个按钮:开始和停止。 两者都可以由全局热键触发。 这是开始按钮的代码(与停止按钮相同,此代码在 Form1 上):
if (keyPressed == Key.F1 && btnStart.Enabled == true)
{
// do stuff in here
}
在 Form2 上,用户应该自己为 Form1 上的按钮设置热键。
该过程应如下所示:
我的想法是这样的:
string userhotkey;
if (keyPressed == userhotkey && btnStart.Enabled == true)
{
// do stuff
}
我的问题是我如何才能意识到用户可以在 Form2 上选择自己的热键并将它们应用于 Form1。
您可以在程序 class 中声明 static 变量:
namespace TestNamespace {
static class Program {
public static String HotkeyStart; //or not string
public static String HotkeyStop; //or not string
[STAThread]
static void Main() {
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new TestForm());
}
}
}
他们可以通过任何形式或 class
我建议您将热键信息存储在 class 作为 uint 或 Keys
我使用类似的东西
public uint Hotkey = 0;
public uint Modifier = 0;
public void SetHotkey(uint key) {
Hotkey = key & 0xffff;
Modifier = Convert.ToUInt32(String.Concat(Convert.ToString(key >> 16 & 7, 2).PadLeft(3, '0').Reverse()), 2) | 0x4000;
RegisterHotkey(true);
}
public override string ToString() {
String ret = "";
if ((Modifier & 1) == 1)
ret += "Alt+";
if ((Modifier & 2) == 2)
ret += "Ctrl+";
if ((Modifier & 4) == 4)
ret += "Shift+";
ret += (Keys)Hotkey;
if (IsActive) {
return ret;
}
else {
return ret+ "(Not active)";
}
}
我使用的这种格式(热键+修饰符)
[DllImport("user32.dll")]
public static extern
bool RegisterHotKey(IntPtr hWnd, int id, uint fsModifiers, int vk);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.