OSDN Git Service

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