2017-03-15 30 views
0

我的笔记本电脑不具备的“FN”热键快速前进到下一首曲目在Windows Media Player /林等添加Windows 10热键命令发送到运行的应用程序(沟)

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

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

+0

由于您的问题,我将很快将自定义热键操作添加到我的应用程序中。现在它与虚拟桌面相关。检查出来.... https://github.com/mzomparelli/zVirtualDesktop –

回答

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 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); 

    } 

} 
相关问题