簡體   English   中英

在多個 forms 上選擇並保存您自己的熱鍵

[英]Choosing & saving your own hotkeys on multiple forms

目前我有兩個按鈕:開始和停止。 兩者都可以由全局熱鍵觸發。 這是開始按鈕的代碼(與停止按鈕相同,此代碼在 Form1 上):

if (keyPressed == Key.F1 && btnStart.Enabled == true)
{
    // do stuff in here
}

在 Form2 上,用戶應該自己為 Form1 上的按鈕設置熱鍵。

該過程應如下所示:

  1. 向用戶顯示當前熱鍵(F1 啟動和 F2 停止),例如使用 label
  2. 更改熱鍵的按鈕,例如“按此按鈕,然后單擊鍵盤上您想要的鍵”->“新熱鍵現在是 F5!”
  3. 將新的熱鍵保存在一個字符串(或類似的)中
  4. 保存熱鍵,以便在應用程序重新啟動后,用戶選擇的熱鍵處於活動狀態 & 而不再是 F1 和 F2。

我的想法是這樣的:

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM