Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无窗口.NET应用程序中的全局热键_.net_Keyboard Shortcuts - Fatal编程技术网

无窗口.NET应用程序中的全局热键

无窗口.NET应用程序中的全局热键,.net,keyboard-shortcuts,.net,Keyboard Shortcuts,我已经读过关于这个问题的类似问题,包括和。我还调查了一个NuGet软件包,它似乎还处于初级阶段 这里的问题是,它们中的大多数似乎是为Winforms设计的,或者可能在WPF中运行。他们正在使用的P/Invoke似乎需要一个窗口句柄。我正在考虑在这里使用windoless应用程序,即在没有主窗体或窗口的情况下运行,除非按下某个组合键,因此实际上可能没有句柄 那么,传递厚颜无耻的0作为p/Invoke的窗口句柄是否会导致它不寻找处理按键的窗口?或者,我的最佳选择是使用不可见的不可聚焦窗口 为了增加一

我已经读过关于这个问题的类似问题,包括和。我还调查了一个NuGet软件包,它似乎还处于初级阶段

这里的问题是,它们中的大多数似乎是为Winforms设计的,或者可能在WPF中运行。他们正在使用的P/Invoke似乎需要一个窗口句柄。我正在考虑在这里使用windoless应用程序,即在没有主窗体或窗口的情况下运行,除非按下某个组合键,因此实际上可能没有句柄

那么,传递厚颜无耻的0作为p/Invoke的窗口句柄是否会导致它不寻找处理按键的窗口?或者,我的最佳选择是使用不可见的不可聚焦窗口

为了增加一点内容,我正在制作一个无窗口应用程序,与TTS一起使用,TTS提供控制反馈,我这里的目标受众是盲人和视力受损的用户。有时,需要输入一些内容,因此我希望能够在必要时启动表单,但在大多数情况下,我希望没有窗口将内容弄乱

一些示例代码(我无法验证这是否可以正常工作)

感谢您提供的帮助。

使用RegisterHotkey()是检测热键按下的样板。我不会在这里重复,您可以很容易地从函数名中搜索示例代码


不过,它确实需要一个窗口,没有解决方法。它不必是一个可见的窗口。到目前为止,创建不可见窗口的最简单方法是使用表单类,就像在任何Winforms应用程序中一样,并设置ShowInTaskbar=False、WindowsState=Minimized、FormBorderStyle=FixedToolWindow。在OnLoad()方法重写中调用RegisterHotkey(),需要Handle属性。简单明了。

只需创建全局kbhook,就像您也可以使用该类一样。在下面的示例中,我还使用了,这是启动“无窗口”应用程序的好方法:

static class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new MyContext());
    }
}

public class MyContext : ApplicationContext
{

    private MyHotKey hotkeys = null;

    public MyContext()
    {
        hotkeys = new MyHotKey();
        hotkeys.HotkeyPressed += new MyHotKey.HotkeyDelegate(hotkeys_HotkeyPressed);
    }

    private void hotkeys_HotkeyPressed(int ID)
    {
        switch (ID)
        {
            case 1001:
                MessageBox.Show("Alt-1");
                break;

            case 1002:
                MessageBox.Show("Alt-2");
                break;

            case 1003: // Alt-Q
                Application.Exit();
                break;
            default:
                break;
        }
    }

}

public class MyHotKey : NativeWindow
{

    private const int WM_HOTKEY = 0x0312;
    private const int WM_DESTROY = 0x0002;

    [System.Runtime.InteropServices.DllImport("user32.dll")]
    public static extern bool RegisterHotKey(IntPtr hWnd, int id, int fsModifiers, int vlc);
    [System.Runtime.InteropServices.DllImport("user32.dll")]
    public static extern bool UnregisterHotKey(IntPtr hWnd, int id);

    private List<Int32> IDs = new List<int>();

    public delegate void HotkeyDelegate(int ID);
    public event HotkeyDelegate HotkeyPressed;

    public MyHotKey()
    {
        this.CreateHandle(new CreateParams());
        Application.ApplicationExit += new EventHandler(Application_ApplicationExit);

        RegisterCombo(1001, 1, (int)Keys.D1);
        RegisterCombo(1002, 1, (int)Keys.D2);
        RegisterCombo(1003, 1, (int)Keys.Q);
    }

    private void RegisterCombo(Int32 ID, int fsModifiers, int vlc)
    {
        if (RegisterHotKey(this.Handle, ID, fsModifiers, vlc))
        {
            IDs.Add(ID);
        }
    }

    private void Application_ApplicationExit(object sender, EventArgs e)
    {
        this.DestroyHandle();
    }

    protected override void WndProc(ref Message m)
    {
        switch (m.Msg)
        {
            case WM_HOTKEY:
                if (HotkeyPressed != null)
                {
                    HotkeyPressed(m.WParam.ToInt32());
                }
                break;

            case WM_DESTROY: // fires when "Application.Exit();" is called
                foreach (int ID in IDs)
                {
                    UnregisterHotKey(this.Handle, ID);
                }
                break;
        }
        base.WndProc(ref m);
    }

}
静态类程序
{
/// 
///应用程序的主要入口点。
/// 
[状态线程]
静态void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
运行(新的MyContext());
}
}
公共类MyContext:ApplicationContext
{
private MyHotKey hotkeys=null;
公共MyContext()
{
热键=新的我的热键();
hotkeys.HotkeyPressed+=新建我的热键.HotkeyDelegate(热键\热键按下);
}
私有无效热键\u热键按下(int ID)
{
开关(ID)
{
案例1001:
MessageBox.Show(“Alt-1”);
打破
案例1002:
MessageBox.Show(“Alt-2”);
打破
案例1003://Alt-Q
Application.Exit();
打破
违约:
打破
}
}
}
公共类My热键:NativeWindow
{
私有常量int WM_热键=0x0312;
私有常量int WM_DESTROY=0x0002;
[System.Runtime.InteropServices.DllImport(“user32.dll”)]
公共静态外部布尔寄存器hotkey(IntPtr hWnd、intid、intfsmodifiers、intvlc);
[System.Runtime.InteropServices.DllImport(“user32.dll”)]
公共静态外部bool unregister热键(IntPtr hWnd,intid);
私有列表ID=新列表();
公共委托无效HotkeyDelegate(int ID);
公共事件热键代表热键按下;
公共MyHotKey()
{
this.CreateHandle(新的CreateParams());
Application.ApplicationExit+=新的事件处理程序(Application\u ApplicationExit);
寄存器COMBO(1001,1,(int)键D1);
RegisterCombo(1002,1,(int)key.D2);
RegisterCombo(1003,1,(int)key.Q);
}
私有无效寄存器COMBO(Int32 ID、int fsModifiers、int vlc)
{
if(RegisterHotKey(this.Handle,ID,fsModifiers,vlc))
{
添加(ID);
}
}
私有void应用程序\u ApplicationExit(对象发送方,事件参数e)
{
这个.handle();
}
受保护的覆盖无效WndProc(参考消息m)
{
开关(m.Msg)
{
案例WM_热键:
如果(热键按下!=null)
{
热键按下(m.WParam.ToInt32());
}
打破
案例WM_DESTROY://在调用“Application.Exit();”时激发
foreach(ID中的int-ID)
{
取消注册热键(this.Handle,ID);
}
打破
}
基准WndProc(参考m);
}
}

通过阅读其他问题,人们非常反对勾搭。挂钩是否有坏处?PeuZunkys:链接不工作…链接不工作,我已经给出了一个想法,一个GO,它仍然出现在ALT选项卡列表的应用程序,人们仍然可以提出空白表格,因为它只是最小化。不过,我会看看我是否能从中构建出真正的解决方案。由于这个方法使用表单,我可以借用已有的代码,所以它有这个优势。从Win7开始,我就完全停止使用Alt+Tab:)嗯,我不知道它是怎么做到的,但它做到了:)我会继续检查,以确保它确实不碍事,但我认为这是一个winnerI。如果我运气不好,我仍然可以找到这个窗口。如果我关闭所有其他窗口,这似乎使焦点不知从何而来。我已尝试在窗体激活时将窗口状态设置为最小化。还有其他建议吗?你对“无窗口应用”相当坚决。现在你改变主意了,你真的想要一扇窗户吗?单击“提问”按钮解释您真正想要的内容。这看起来与我看到的其他代码类似,只是它使用了ApplicationContext。我认为这是另一种选择。虽然它不像Winforms的代码那么容易理解,但它并不依赖于表单,这让人感觉不太像是一个小步。
static class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new MyContext());
    }
}

public class MyContext : ApplicationContext
{

    private MyHotKey hotkeys = null;

    public MyContext()
    {
        hotkeys = new MyHotKey();
        hotkeys.HotkeyPressed += new MyHotKey.HotkeyDelegate(hotkeys_HotkeyPressed);
    }

    private void hotkeys_HotkeyPressed(int ID)
    {
        switch (ID)
        {
            case 1001:
                MessageBox.Show("Alt-1");
                break;

            case 1002:
                MessageBox.Show("Alt-2");
                break;

            case 1003: // Alt-Q
                Application.Exit();
                break;
            default:
                break;
        }
    }

}

public class MyHotKey : NativeWindow
{

    private const int WM_HOTKEY = 0x0312;
    private const int WM_DESTROY = 0x0002;

    [System.Runtime.InteropServices.DllImport("user32.dll")]
    public static extern bool RegisterHotKey(IntPtr hWnd, int id, int fsModifiers, int vlc);
    [System.Runtime.InteropServices.DllImport("user32.dll")]
    public static extern bool UnregisterHotKey(IntPtr hWnd, int id);

    private List<Int32> IDs = new List<int>();

    public delegate void HotkeyDelegate(int ID);
    public event HotkeyDelegate HotkeyPressed;

    public MyHotKey()
    {
        this.CreateHandle(new CreateParams());
        Application.ApplicationExit += new EventHandler(Application_ApplicationExit);

        RegisterCombo(1001, 1, (int)Keys.D1);
        RegisterCombo(1002, 1, (int)Keys.D2);
        RegisterCombo(1003, 1, (int)Keys.Q);
    }

    private void RegisterCombo(Int32 ID, int fsModifiers, int vlc)
    {
        if (RegisterHotKey(this.Handle, ID, fsModifiers, vlc))
        {
            IDs.Add(ID);
        }
    }

    private void Application_ApplicationExit(object sender, EventArgs e)
    {
        this.DestroyHandle();
    }

    protected override void WndProc(ref Message m)
    {
        switch (m.Msg)
        {
            case WM_HOTKEY:
                if (HotkeyPressed != null)
                {
                    HotkeyPressed(m.WParam.ToInt32());
                }
                break;

            case WM_DESTROY: // fires when "Application.Exit();" is called
                foreach (int ID in IDs)
                {
                    UnregisterHotKey(this.Handle, ID);
                }
                break;
        }
        base.WndProc(ref m);
    }

}