[英]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.