繁体   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