1 // OpenTween - Client of Twitter
2 // Copyright (c) 2007-2011 kiri_feather (@kiri_feather) <kiri.feather@gmail.com>
3 // (c) 2008-2011 Moz (@syo68k)
4 // (c) 2008-2011 takeshik (@takeshik) <http://www.takeshik.org/>
5 // (c) 2010-2011 anis774 (@anis774) <http://d.hatena.ne.jp/anis774/>
6 // (c) 2010-2011 fantasticswallow (@f_swallow) <http://twitter.com/f_swallow>
7 // (c) 2011 Egtra (@egtra) <http://dev.activebasic.com/egtra/>
8 // All rights reserved.
10 // This file is part of OpenTween.
12 // This program is free software; you can redistribute it and/or modify it
13 // under the terms of the GNU General Public License as published by the Free
14 // Software Foundation; either version 3 of the License, or (at your option)
17 // This program is distributed in the hope that it will be useful, but
18 // WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 // or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 // You should have received a copy of the GNU General Public License along
23 // with this program. If not, see <http://www.gnu.org/licenses/>, or write to
24 // the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
25 // Boston, MA 02110-1301, USA.
28 using System.Collections.Generic;
31 using System.Windows.Forms;
35 public class HookGlobalHotkey : NativeWindow, IDisposable
37 private readonly Form _targetForm;
38 private class KeyEventValue
40 public KeyEventArgs KeyEvent { get; }
41 public int Value { get; }
43 public KeyEventValue(KeyEventArgs keyEvent, int Value)
45 this.KeyEvent = keyEvent;
50 private readonly Dictionary<int, KeyEventValue> _hotkeyID;
62 public event KeyEventHandler HotkeyPressed;
64 protected override void WndProc(ref Message m)
66 const int WM_HOTKEY = 0x312;
67 if (m.Msg == WM_HOTKEY)
69 if (_hotkeyID.ContainsKey(m.WParam.ToInt32()))
71 HotkeyPressed?.Invoke(this, _hotkeyID[m.WParam.ToInt32()].KeyEvent);
78 public HookGlobalHotkey(Form targetForm)
80 _targetForm = targetForm;
81 _hotkeyID = new Dictionary<int, KeyEventValue>();
83 _targetForm.HandleCreated += this.OnHandleCreated;
84 _targetForm.HandleDestroyed += this.OnHandleDestroyed;
87 public void OnHandleCreated(Object sender, EventArgs e)
88 => this.AssignHandle(_targetForm.Handle);
90 public void OnHandleDestroyed(Object sender, EventArgs e)
91 => this.ReleaseHandle();
93 public bool RegisterOriginalHotkey(Keys hotkey, int hotkeyValue, ModKeys modifiers)
95 var modKey = Keys.None;
96 if ((modifiers & ModKeys.Alt) == ModKeys.Alt) modKey |= Keys.Alt;
97 if ((modifiers & ModKeys.Ctrl) == ModKeys.Ctrl) modKey |= Keys.Control;
98 if ((modifiers & ModKeys.Shift) == ModKeys.Shift) modKey |= Keys.Shift;
99 if ((modifiers & ModKeys.Win) == ModKeys.Win) modKey |= Keys.LWin;
100 var key = new KeyEventArgs(hotkey | modKey);
101 foreach (var (_, value) in this._hotkeyID)
103 if (value.KeyEvent.KeyData == key.KeyData && value.Value == hotkeyValue) return true; // 登録済みなら正常終了
105 var hotkeyId = NativeMethods.RegisterGlobalHotKey(hotkeyValue, (int)modifiers, this._targetForm);
108 this._hotkeyID.Add(hotkeyId, new KeyEventValue(key, hotkeyValue));
114 public void UnregisterAllOriginalHotkey()
116 foreach (ushort hotkeyId in this._hotkeyID.Keys)
118 NativeMethods.UnregisterGlobalHotKey(hotkeyId, this._targetForm);
120 this._hotkeyID.Clear();
123 private bool disposedValue = false; // 重複する呼び出しを検出するには
126 protected virtual void Dispose(bool disposing)
128 if (!this.disposedValue)
134 if (this._targetForm != null && !this._targetForm.IsDisposed)
136 this.UnregisterAllOriginalHotkey();
137 _targetForm.HandleCreated -= this.OnHandleCreated;
138 _targetForm.HandleDestroyed -= this.OnHandleDestroyed;
141 this.disposedValue = true;
144 #region " IDisposable Support "
145 // このコードは、破棄可能なパターンを正しく実装できるように Visual Basic によって追加されました。
146 public void Dispose()
148 // このコードを変更しないでください。クリーンアップ コードを上の Dispose(bool disposing) に記述します。
150 GC.SuppressFinalize(this);
155 => this.Dispose(false);