简体   繁体   English

如何创建通用键盘快捷键?

[英]How to create a universal keyboard shortcut?

I have searched online for this, but can't find nothing at all. 我在网上搜索过这个,但根本找不到任何东西。 What I would like to do is to create a keyboard shortcut that I would be able to use in all applications. 我想要做的是创建一个键盘快捷方式,我将能够在所有应用程序中使用。 A universal keyboard shortcut, so that when I press, say Ctrl + Shift + X in any application, it would execute a piece of code I created in C#. 一个通用的键盘快捷键,所以当我按下时,在任何应用程序中说Ctrl + Shift + X ,它将执行我在C#中创建的一段代码。 For example, when I'm in Skype, I would select text and press Ctrl + Shift + X (or whatever other key combination), it would change the color of the text from black to blue. 例如,当我在Skype时,我会选择文本并按Ctrl + Shift + X (或任何其他组合键),它会将文本的颜色从黑色更改为蓝色。 That is just an example to try and explain what I want to do. 这只是尝试解释我想做什么的一个例子。 I'm thinking I would have to import a DLL and edit that (maybe user32.dll?) I'm just guessing. 我想我必须导入一个DLL并编辑它(也许是user32.dll?)我只是在猜测。 I have no clue how to do this, so any help will be greatly appreciated! 我不知道如何做到这一点,所以任何帮助将不胜感激!

Thanks a lot in advance :) 非常感谢提前:)

PS: I am using Windows Forms Application, .NET Framework 4.0. PS:我使用的是Windows Forms Application,.NET Framework 4.0。 Unclear about something I am trying to do/say? 不清楚我想做什么/说什么? Please feel free to comment and I will get back to you right away. 请随时发表评论,我会立即回复您。

Win32 has a RegisterHotKey function as part of the Win32 API. Win32具有RegisterHotKey函数作为Win32 API的一部分。 To use it in managed code (C#), you'd have to pInvoke it. 要在托管代码(C#)中使用它,您必须pInvoke它。 Here is an example: 这是一个例子:

public class WindowsShell
{
    #region fields
    public static int MOD_ALT = 0x1;
    public static int MOD_CONTROL = 0x2;
    public static int MOD_SHIFT = 0x4;
    public static int MOD_WIN = 0x8;
    public static int WM_HOTKEY = 0x312;
    #endregion

    [DllImport("user32.dll")]
    private static extern bool RegisterHotKey(IntPtr hWnd, int id, int fsModifiers, int vlc);

    [DllImport("user32.dll")]
    private static extern bool UnregisterHotKey(IntPtr hWnd, int id);

    private static int keyId;
    public static void RegisterHotKey(Form f, Keys key)
    {
        int modifiers = 0;

        if ((key & Keys.Alt) == Keys.Alt)
            modifiers = modifiers | WindowsShell.MOD_ALT;

        if ((key & Keys.Control) == Keys.Control)
            modifiers = modifiers | WindowsShell.MOD_CONTROL;

        if ((key & Keys.Shift) == Keys.Shift)
            modifiers = modifiers | WindowsShell.MOD_SHIFT;

        Keys k = key & ~Keys.Control & ~Keys.Shift & ~Keys.Alt;        
        keyId = f.GetHashCode(); // this should be a key unique ID, modify this if you want more than one hotkey
        RegisterHotKey((IntPtr)f.Handle, keyId, (uint)modifiers, (uint)k);
    }

    private delegate void Func();

    public static void UnregisterHotKey(Form f)
    {
        try
        {
            UnregisterHotKey(f.Handle, keyId); // modify this if you want more than one hotkey
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }
    }
}

public partial class Form1 : Form, IDisposable
{
    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);
        Keys k = Keys.A | Keys.Control;
        WindowsShell.RegisterHotKey(this, k);
    }

    // CF Note: The WndProc is not present in the Compact Framework (as of vers. 3.5)! please derive from the MessageWindow class in order to handle WM_HOTKEY
    protected override void WndProc(ref Message m)
    {
        base.WndProc(ref m);

        if (m.Msg == WindowsShell.WM_HOTKEY)
            this.Visible = !this.Visible;
    }

    private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {
        WindowsShell.UnregisterHotKey(this);
    }
}

This code came from this article . 此代码来自本文 Read that article for more information and more examples. 阅读该文章以获取更多信息和更多示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM