OSDN Git Service

event, delegate に対するnullableアノテーションを追加
[opentween/open-tween.git] / OpenTween / HookGlobalHotkey.cs
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.
9 //
10 // This file is part of OpenTween.
11 //
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)
15 // any later version.
16 //
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
20 // for more details.
21 //
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.
26
27 #nullable enable
28
29 using System;
30 using System.Collections.Generic;
31 using System.Linq;
32 using System.Text;
33 using System.Windows.Forms;
34
35 namespace OpenTween
36 {
37     public class HookGlobalHotkey : NativeWindow, IDisposable
38     {
39         private readonly Form _targetForm;
40         private class KeyEventValue
41         {
42             public KeyEventArgs KeyEvent { get; }
43             public int Value { get; }
44
45             public KeyEventValue(KeyEventArgs keyEvent, int Value)
46             {
47                 this.KeyEvent = keyEvent;
48                 this.Value = Value;
49             }
50         }
51
52         private readonly Dictionary<int, KeyEventValue> _hotkeyID;
53
54         [Flags]
55         public enum ModKeys
56         {
57             None = 0,
58             Alt = 0x1,
59             Ctrl = 0x2,
60             Shift = 0x4,
61             Win = 0x8,
62         }
63
64         public event KeyEventHandler? HotkeyPressed;
65
66         protected override void WndProc(ref Message m)
67         {
68             const int WM_HOTKEY = 0x312;
69             if (m.Msg == WM_HOTKEY)
70             {
71                 if (_hotkeyID.ContainsKey(m.WParam.ToInt32()))
72                 {
73                     HotkeyPressed?.Invoke(this, _hotkeyID[m.WParam.ToInt32()].KeyEvent);
74                 }
75                 return;
76             }
77             base.WndProc(ref m);
78         }
79
80         public HookGlobalHotkey(Form targetForm)
81         {
82             _targetForm = targetForm;
83             _hotkeyID = new Dictionary<int, KeyEventValue>();
84
85            _targetForm.HandleCreated += this.OnHandleCreated;
86            _targetForm.HandleDestroyed += this.OnHandleDestroyed;
87         }
88
89         public void OnHandleCreated(object sender, EventArgs e)
90             => this.AssignHandle(_targetForm.Handle);
91
92         public void OnHandleDestroyed(object sender, EventArgs e)
93             => this.ReleaseHandle();
94
95         public bool RegisterOriginalHotkey(Keys hotkey, int hotkeyValue, ModKeys modifiers)
96         {
97             var modKey = Keys.None;
98             if ((modifiers & ModKeys.Alt) == ModKeys.Alt) modKey |= Keys.Alt;
99             if ((modifiers & ModKeys.Ctrl) == ModKeys.Ctrl) modKey |= Keys.Control;
100             if ((modifiers & ModKeys.Shift) == ModKeys.Shift) modKey |= Keys.Shift;
101             if ((modifiers & ModKeys.Win) == ModKeys.Win) modKey |= Keys.LWin;
102             var key = new KeyEventArgs(hotkey | modKey);
103             foreach (var (_, value) in this._hotkeyID)
104             {
105                 if (value.KeyEvent.KeyData == key.KeyData && value.Value == hotkeyValue) return true; // 登録済みなら正常終了
106             }
107             var hotkeyId = NativeMethods.RegisterGlobalHotKey(hotkeyValue, (int)modifiers, this._targetForm);
108             if (hotkeyId > 0)
109             {
110                 this._hotkeyID.Add(hotkeyId, new KeyEventValue(key, hotkeyValue));
111                 return true;
112             }
113             return false;
114         }
115
116         public void UnregisterAllOriginalHotkey()
117         {
118             foreach (ushort hotkeyId in this._hotkeyID.Keys)
119             {
120                 NativeMethods.UnregisterGlobalHotKey(hotkeyId, this._targetForm);
121             }
122             this._hotkeyID.Clear();
123         }
124
125         private bool disposedValue = false;        // 重複する呼び出しを検出するには
126
127         // IDisposable
128         protected virtual void Dispose(bool disposing)
129         {
130             if (!this.disposedValue)
131             {
132                 if (disposing)
133                 {
134                 }
135
136                 if (this._targetForm != null && !this._targetForm.IsDisposed)
137                 {
138                     this.UnregisterAllOriginalHotkey();
139                     _targetForm.HandleCreated -= this.OnHandleCreated;
140                     _targetForm.HandleDestroyed -= this.OnHandleDestroyed;
141                 }
142             }
143             this.disposedValue = true;
144         }
145
146 #region " IDisposable Support "
147         // このコードは、破棄可能なパターンを正しく実装できるように Visual Basic によって追加されました。
148         public void Dispose()
149         {
150             // このコードを変更しないでください。クリーンアップ コードを上の Dispose(bool disposing) に記述します。
151             Dispose(true);
152             GC.SuppressFinalize(this);
153         }
154 #endregion
155
156         ~HookGlobalHotkey()
157             => this.Dispose(false);
158     }
159 }