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.
30 using System.Collections.Generic;
33 using System.Windows.Forms;
37 public class HookGlobalHotkey : NativeWindow, IDisposable
39 private readonly Form targetForm;
41 private readonly record struct KeyEventValue(
42 KeyEventArgs KeyEvent,
46 private readonly Dictionary<int, KeyEventValue> hotkeyID;
58 public event KeyEventHandler? HotkeyPressed;
60 protected override void WndProc(ref Message m)
62 const int WM_HOTKEY = 0x312;
63 if (m.Msg == WM_HOTKEY)
65 if (this.hotkeyID.ContainsKey(m.WParam.ToInt32()))
67 this.HotkeyPressed?.Invoke(this, this.hotkeyID[m.WParam.ToInt32()].KeyEvent);
74 public HookGlobalHotkey(Form targetForm)
76 this.targetForm = targetForm;
77 this.hotkeyID = new Dictionary<int, KeyEventValue>();
79 this.targetForm.HandleCreated += this.OnHandleCreated;
80 this.targetForm.HandleDestroyed += this.OnHandleDestroyed;
83 public void OnHandleCreated(object sender, EventArgs e)
84 => this.AssignHandle(this.targetForm.Handle);
86 public void OnHandleDestroyed(object sender, EventArgs e)
87 => this.ReleaseHandle();
89 public bool RegisterOriginalHotkey(Keys hotkey, int hotkeyValue, ModKeys modifiers)
91 var modKey = Keys.None;
92 if ((modifiers & ModKeys.Alt) == ModKeys.Alt) modKey |= Keys.Alt;
93 if ((modifiers & ModKeys.Ctrl) == ModKeys.Ctrl) modKey |= Keys.Control;
94 if ((modifiers & ModKeys.Shift) == ModKeys.Shift) modKey |= Keys.Shift;
95 if ((modifiers & ModKeys.Win) == ModKeys.Win) modKey |= Keys.LWin;
96 var key = new KeyEventArgs(hotkey | modKey);
97 foreach (var (_, value) in this.hotkeyID)
99 if (value.KeyEvent.KeyData == key.KeyData && value.Value == hotkeyValue) return true; // 登録済みなら正常終了
101 var hotkeyId = NativeMethods.RegisterGlobalHotKey(hotkeyValue, (int)modifiers, this.targetForm);
104 this.hotkeyID.Add(hotkeyId, new KeyEventValue(key, hotkeyValue));
110 public void UnregisterAllOriginalHotkey()
112 foreach (ushort hotkeyId in this.hotkeyID.Keys)
114 NativeMethods.UnregisterGlobalHotKey(hotkeyId, this.targetForm);
116 this.hotkeyID.Clear();
119 private bool disposedValue = false; // 重複する呼び出しを検出するには
122 protected virtual void Dispose(bool disposing)
124 if (!this.disposedValue)
130 if (this.targetForm != null && !this.targetForm.IsDisposed)
132 this.UnregisterAllOriginalHotkey();
133 this.targetForm.HandleCreated -= this.OnHandleCreated;
134 this.targetForm.HandleDestroyed -= this.OnHandleDestroyed;
137 this.disposedValue = true;
140 #region " IDisposable Support "
141 // このコードは、破棄可能なパターンを正しく実装できるように Visual Basic によって追加されました。
142 public void Dispose()
144 // このコードを変更しないでください。クリーンアップ コードを上の Dispose(bool disposing) に記述します。
146 GC.SuppressFinalize(this);
151 => this.Dispose(false);