添加一个Windows 10热键将命令发送到正在运行的应用程序(Groove)

时间:2017-03-15 18:07:21

标签: windows-10 global hotkeys

我的笔记本没有“FN”热键可以快进到Windows Media Player / Grove等的下一首曲目。

有没有人找到一种方法来创建自定义Windows 10全局热键,以模仿大多数笔记本电脑在自己的自定义热键中构建的内容?

我想我找到了一个可以下载的程序来创建它们,但我不想使用其他应用程序。我想知道自己创建命令的内容是什么。

1 个答案:

答案 0 :(得分:0)

我意识到你也需要热键的处理程序代码。我将在棒球练习后回来向您展示如何将该消息发送到Windows Media Player。

您将需要使用Window API。具体来说是RegisterHotKey

[DllImport("user32", SetLastError = true)]
        public static extern int RegisterHotKey(IntPtr hWnd, int id, uint fsModifiers, uint vk);

[DllImport("user32", SetLastError = true)]
        public static extern int UnregisterHotKey(IntPtr hWnd, int id);

这是一个完整的Hotkey课程,可以帮助你。 (本课程中有一些你不需要的东西,但你不必担心它)

在你的情况下,你会像这样使用这个类;

Hotkey keyMoveNext = new Hotkey();
//In the handler you just handle the next track skipping.
keyMoveNext.HotkeyActivated += MyHotkeyActivatedHandler;

    keyMoveNext.Register(Keys.W, true, false, false, true);

就是这样。

using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;

using System.Runtime.InteropServices;
using System.ComponentModel;
using System.Windows.Forms;



public class Hotkey : IDisposable
{

    private bool _IsRegistered = false;

    private HotkeyWindow _Window;

    public int ClipboardNumber;


    public int _ID;
    public string ID;
    public Hotkey()
    {
        System.Threading.Interlocked.Increment(ref _ID);
    }

    public string DesktopNumber()
    {
        return ID;
    }


    public event HotkeyActivatedEventHandler HotkeyActivated;
    public delegate void HotkeyActivatedEventHandler(object sender, System.EventArgs e);

    private bool _modifierALT = false;
    public bool modifierALT
    {
        get
        {
            return _modifierALT;
        }
    }

    private bool _modifierCTRL = false;
    public bool modifierCTRL
    {
        get
        {
            return _modifierCTRL;
        }
    }

    private bool _modifierSHIFT = false;
    public bool modifierSHIFT
    {
        get
        {
            return _modifierSHIFT;
        }
    }

    private bool _modifierWIN = false;
    public bool modifierWIN
    {
        get
        {
            return _modifierWIN;
        }
    }

    private string _key = "";
    public string Key
    {
        get
        {
            return _key;
        }
    }

    public string HotKeyString()
    {
        string keys__1 = "";

        if (_modifierALT)
        {
            if (string.IsNullOrEmpty(keys__1))
            {
                keys__1 = keys__1 + "ALT";
            }
            else
            {
                keys__1 = keys__1 + "+ALT";
            }
        }

        if (_modifierCTRL)
        {
            if (string.IsNullOrEmpty(keys__1))
            {
                keys__1 = keys__1 + "CTRL";
            }
            else
            {
                keys__1 = keys__1 + "+CTRL";
            }
        }

        if (_modifierSHIFT)
        {
            if (string.IsNullOrEmpty(keys__1))
            {
                keys__1 = keys__1 + "SHIFT";
            }
            else
            {
                keys__1 = keys__1 + "+SHIFT";
            }
        }

        if (_modifierWIN)
        {
            if (string.IsNullOrEmpty(keys__1))
            {
                keys__1 = keys__1 + "WIN";
            }
            else
            {
                keys__1 = keys__1 + "+WIN";
            }
        }

        keys__1 = keys__1 + "+" + _key;

        return keys__1;

    }

    public bool Register(System.Windows.Forms.Keys key, bool alt, bool ctrl, bool shift, bool win)
    {
        if (this.IsRegistered)
        {
            this.Unregister();
        }

        _modifierALT = alt;
        _modifierCTRL = ctrl;
        _modifierSHIFT = shift;
        _modifierWIN = win;
        _key = key.ToString();


        //Dim keyAlt As Keys = (key And Keys.Alt)
        //Dim keyControl As Keys = (key And Keys.Control)
        //Dim keyShift As Keys = (key And Keys.Shift)

        uint Modifiers = 0;
        if (alt)
        {
            Modifiers += NativeMethods.MOD_ALT;
        }
        if (ctrl)
        {
            Modifiers += NativeMethods.MOD_CONTROL;
        }
        if (shift)
        {
            Modifiers += NativeMethods.MOD_SHIFT;
        }
        if (win)
        {
            Modifiers += NativeMethods.MOD_WIN;
        }
        //If (keyAlt = Keys.Alt) Then modValue += NativeMethods.MOD_ALT
        //If (keyControl = Keys.Control) Then modValue += NativeMethods.MOD_CONTROL
        //If (keyShift = Keys.Shift) Then modValue += NativeMethods.MOD_SHIFT
        uint keyValue = Convert.ToUInt32(key);
        //- CUInt(keyAlt) - CUInt(keyControl) - CUInt(keyShift)

        this._Window = new HotkeyWindow();
        this._Window.CreateHandle(new CreateParams());
        this._Window.HotkeyMessage += Window_HotkeyMessage;

        if (NativeMethods.RegisterHotKey(this._Window.Handle, _ID, Modifiers, keyValue) == 0)
        {
            MessageBox.Show(HotKeyString() + Convert.ToString(" hotkey is already registered."));
            return false;
            //Environment.[Exit](0)
        }
        else
        {
            _IsRegistered = true;
            return true;

        }
        //Me._IsRegistered = Not (NativeMethods.RegisterHotKey(Me._Window.Handle, _ID, modValue, keyValue) = 0)
    }

    public void Unregister()
    {
        if ((this.IsRegistered))
        {
            this._IsRegistered = (NativeMethods.UnregisterHotKey(this._Window.Handle, _ID) == 0);
            if ((this._IsRegistered == false))
            {
                this._Window.DestroyHandle();
                this._Window = null;
            }
        }
    }

    public bool IsRegistered
    {
        get { return this._IsRegistered; }
    }


    private void Window_HotkeyMessage(object sender, System.EventArgs e)
    {
        if (HotkeyActivated != null)
        {
            HotkeyActivated(this, new System.EventArgs());
        }
        //MessageBox.Show("Hit");
    }

    #region " IDisposable Support "

    // To detect redundant calls

    private bool disposedValue;
    // IDisposable
    protected virtual void Dispose(bool disposing)
    {
        if (!this.disposedValue)
        {
            if (disposing)
            {
                if ((this.IsRegistered == true))
                {
                    this.Unregister();
                }
            }
        }
        this.disposedValue = true;
    }

    public void Dispose()
    {
        Dispose(true);
        GC.SuppressFinalize(this);
    }

    private void IDisposable_Dispose()
    {
        Dispose(true);
    }
    void IDisposable.Dispose()
    {
        IDisposable_Dispose();
    }
    #endregion


    private class HotkeyWindow : NativeWindow
    {

        internal event HotkeyMessageEventHandler HotkeyMessage;
        internal delegate void HotkeyMessageEventHandler(object sender, System.EventArgs e);


        protected override void WndProc(ref System.Windows.Forms.Message m)
        {
            switch (m.Msg)
            {
                case NativeMethods.WM_HOTKEY:
                    if (HotkeyMessage != null)
                    {
                        HotkeyMessage(this, new System.EventArgs());
                    }
                    break;

            }
            base.WndProc(ref m);
        }

    }


    public class NativeMethods
    {

        private NativeMethods()
        {
        }

        internal const uint MOD_ALT = 0x1;
        internal const uint MOD_CONTROL = 0x2;
        internal const uint MOD_SHIFT = 0x4;

        internal const uint MOD_WIN = 0x8;

        internal const int WM_HOTKEY = 0x312;
        [DllImport("kernel32", EntryPoint = "GlobalAddAtom", SetLastError = true, ExactSpelling = false)]
        public static extern int GlobalAddAtom([MarshalAs(UnmanagedType.LPTStr)]
string lpString);


        [DllImport("user32", SetLastError = true)]
        public static extern int RegisterHotKey(IntPtr hWnd, int id, uint fsModifiers, uint vk);


        [DllImport("user32", SetLastError = true)]
        public static extern int UnregisterHotKey(IntPtr hWnd, int id);

    }

}