b7744b6a41f205dccfed409f867d3f6a22a354ed
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / public / Key.cs
1 /*
2  * Copyright(c) 2017 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 using System.ComponentModel;
19
20 namespace Tizen.NUI
21 {
22
23     /// <summary>
24     /// The key structure is used to store a key press.
25     /// </summary>
26     /// <since_tizen> 3 </since_tizen>
27     public class Key : global::System.IDisposable
28     {
29         /// <summary>
30         /// swigCMemOwn
31         /// </summary>
32         /// <since_tizen> 3 </since_tizen>
33         protected bool swigCMemOwn;
34
35         /// <summary>
36         /// A Flat to check if it is already disposed.
37         /// </summary>
38         /// <since_tizen> 3 </since_tizen>
39         protected bool disposed = false;
40
41         private global::System.Runtime.InteropServices.HandleRef swigCPtr;
42
43         //A Flag to check who called Dispose(). (By User or DisposeQueue)
44         private bool isDisposeQueued = false;
45
46         /// <summary>
47         /// The default constructor.
48         /// </summary>
49         /// <since_tizen> 3 </since_tizen>
50         public Key() : this(Interop.Key.new_Key__SWIG_0(), true)
51         {
52             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
53         }
54
55         /// <summary>
56         /// The constructor.
57         /// </summary>
58         /// <param name="keyName">The name of the key pressed or command from the IMF, if later, then the following parameters will be needed.</param>
59         /// <param name="keyString">A string of input characters or key pressed.</param>
60         /// <param name="keyCode">The unique key code for the key pressed.</param>
61         /// <param name="keyModifier">The key modifier for special keys like Shift and Alt.</param>
62         /// <param name="timeStamp">The time (in ms) that the key event occurred.</param>
63         /// <param name="keyState">The state of the key event.</param>
64         internal Key(string keyName, string keyString, int keyCode, int keyModifier, uint timeStamp, Key.StateType keyState) : this(Interop.Key.new_Key__SWIG_1(keyName, keyString, keyCode, keyModifier, timeStamp, (int)keyState), true)
65         {
66             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
67         }
68
69         internal Key(global::System.IntPtr cPtr, bool cMemoryOwn)
70         {
71             swigCMemOwn = cMemoryOwn;
72             swigCPtr = new global::System.Runtime.InteropServices.HandleRef(this, cPtr);
73         }
74
75         /// <summary>
76         /// Dispose.
77         /// </summary>
78         /// <since_tizen> 3 </since_tizen>
79         ~Key()
80         {
81             if (!isDisposeQueued)
82             {
83                 isDisposeQueued = true;
84                 DisposeQueue.Instance.Add(this);
85             }
86         }
87
88         /// <summary>
89         /// Enumeration for specifying the state of the key event.
90         /// </summary>
91         /// <since_tizen> 3 </since_tizen>
92         public enum StateType
93         {
94             /// <summary>
95             /// Key Down.
96             /// </summary>
97             /// <since_tizen> 3 </since_tizen>
98             Down,
99             /// <summary>
100             /// Key Up.
101             /// </summary>
102             /// <since_tizen> 3 </since_tizen>
103             Up,
104             /// <summary>
105             /// Key Last.
106             /// </summary>
107             /// <since_tizen> 3 </since_tizen>
108             Last
109         }
110
111         /// <summary>
112         /// Device name
113         /// </summary>
114         /// <since_tizen> 3 </since_tizen>
115         public string DeviceName
116         {
117             get
118             {
119                 string ret = Interop.NDalic.GetDeviceName(swigCPtr);
120                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
121                 return ret;
122             }
123         }
124
125         /// <summary>
126         /// Name given to the key pressed.
127         /// </summary>
128         /// <since_tizen> 3 </since_tizen>
129         public string KeyPressedName
130         {
131             get
132             {
133                 return keyPressedName;
134             }
135             set
136             {
137                 keyPressedName = value;
138             }
139         }
140
141         /// <summary>
142         /// Get the logical key string. (eg. shift + 1 == "exclamation")
143         /// </summary>
144         /// <returns>The logical key symbol</returns>
145         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
146         [EditorBrowsable(EditorBrowsableState.Never)]
147         public string LogicalKey
148         {
149             get
150             {
151                 return logicalKey;
152             }
153         }
154
155         /// <summary>
156         /// Get the actual string returned that should be used for input editors.
157         /// </summary>
158         /// <returns>The key string</returns>
159         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
160         [EditorBrowsable(EditorBrowsableState.Never)]
161         public string KeyPressed
162         {
163             get
164             {
165                 return keyPressed;
166             }
167         }
168
169         /// <summary>
170         /// Keycode for the key pressed.
171         /// </summary>
172         /// <since_tizen> 3 </since_tizen>
173         public int KeyCode
174         {
175             get
176             {
177                 return keyCode;
178             }
179             set
180             {
181                 keyCode = value;
182             }
183         }
184
185         /// <summary>
186         /// Special keys like Shift, Alt, and Ctrl which modify the next key pressed.
187         /// </summary>
188         /// <since_tizen> 3 </since_tizen>
189         public int KeyModifier
190         {
191             get
192             {
193                 return keyModifier;
194             }
195             set
196             {
197                 keyModifier = value;
198             }
199         }
200
201         /// <summary>
202         /// The time (in ms) that the key event occurred.
203         /// </summary>
204         /// <since_tizen> 3 </since_tizen>
205         public uint Time
206         {
207             get
208             {
209                 return time;
210             }
211             set
212             {
213                 time = value;
214             }
215         }
216
217         /// <summary>
218         /// State of the key event.
219         /// </summary>
220         /// <since_tizen> 3 </since_tizen>
221         public Key.StateType State
222         {
223             get
224             {
225                 return state;
226             }
227             set
228             {
229                 state = value;
230             }
231         }
232
233         /// <summary>
234         /// Get the device class the key event originated from.
235         /// </summary>
236         /// <since_tizen> 3 </since_tizen>
237         public DeviceClassType DeviceClass
238         {
239             get
240             {
241                 int ret = Interop.NDalic.GetDeviceClass(swigCPtr);
242                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
243                 return (DeviceClassType)ret;
244             }
245         }
246
247         /// <summary>
248         /// Get the device subclass the key event originated from.
249         /// </summary>
250         /// <since_tizen> 4 </since_tizen>
251         public DeviceSubClassType DeviceSubClass
252         {
253             get
254             {
255                 int ret = Interop.NDalic.GetDeviceSubClass(swigCPtr);
256                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
257                 return (DeviceSubClassType)ret;
258             }
259         }
260
261         private string keyPressedName
262         {
263             set
264             {
265                 Interop.Key.Key_keyPressedName_set(swigCPtr, value);
266                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
267             }
268             get
269             {
270                 string ret = Interop.Key.Key_keyPressedName_get(swigCPtr);
271                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
272                 return ret;
273             }
274         }
275
276         private string keyPressed
277         {
278             set
279             {
280                 Interop.Key.Key_keyPressed_set(swigCPtr, value);
281                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
282             }
283             get
284             {
285                 string ret = Interop.Key.Key_keyPressed_get(swigCPtr);
286                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
287                 return ret;
288             }
289         }
290
291         private int keyCode
292         {
293             set
294             {
295                 Interop.Key.Key_keyCode_set(swigCPtr, value);
296                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
297             }
298             get
299             {
300                 int ret = Interop.Key.Key_keyCode_get(swigCPtr);
301                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
302                 return ret;
303             }
304         }
305
306         private int keyModifier
307         {
308             set
309             {
310                 Interop.Key.Key_keyModifier_set(swigCPtr, value);
311                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
312             }
313             get
314             {
315                 int ret = Interop.Key.Key_keyModifier_get(swigCPtr);
316                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
317                 return ret;
318             }
319         }
320
321         private uint time
322         {
323             set
324             {
325                 Interop.Key.Key_time_set(swigCPtr, value);
326                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
327             }
328             get
329             {
330                 uint ret = Interop.Key.Key_time_get(swigCPtr);
331                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
332                 return ret;
333             }
334         }
335
336         private Key.StateType state
337         {
338             set
339             {
340                 Interop.Key.Key_state_set(swigCPtr, (int)value);
341                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
342             }
343             get
344             {
345                 Key.StateType ret = (Key.StateType)Interop.Key.Key_state_get(swigCPtr);
346                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
347                 return ret;
348             }
349         }
350
351         private string logicalKey
352         {
353             get
354             {
355                 string ret = Interop.Key.Key_logicalKey_get(swigCPtr);
356                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
357                 return "";
358             }
359         }
360
361         /// <summary>
362         /// Dispose.
363         /// </summary>
364         /// <since_tizen> 3 </since_tizen>
365         public void Dispose()
366         {
367             //Throw excpetion if Dispose() is called in separate thread.
368             if (!Window.IsInstalled())
369             {
370                 throw new System.InvalidOperationException("This API called from separate thread. This API must be called from MainThread.");
371             }
372
373             if (isDisposeQueued)
374             {
375                 Dispose(DisposeTypes.Implicit);
376             }
377             else
378             {
379                 Dispose(DisposeTypes.Explicit);
380                 System.GC.SuppressFinalize(this);
381             }
382         }
383
384         /// <summary>
385         /// Checks to see if the Shift key modifier has been supplied.
386         /// </summary>
387         /// <returns>True if Shift modifier.</returns>
388         /// <since_tizen> 3 </since_tizen>
389         public bool IsShiftModifier()
390         {
391             bool ret = Interop.Key.Key_IsShiftModifier(swigCPtr);
392             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
393             return ret;
394         }
395
396         /// <summary>
397         /// Checks to see if Ctrl (control) key modifier has been supplied.
398         /// </summary>
399         /// <returns>True if Ctrl modifier.</returns>
400         /// <since_tizen> 3 </since_tizen>
401         public bool IsCtrlModifier()
402         {
403             bool ret = Interop.Key.Key_IsCtrlModifier(swigCPtr);
404             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
405             return ret;
406         }
407
408         /// <summary>
409         /// Checks to see if Alt key modifier has been supplied.
410         /// </summary>
411         /// <returns>True if Alt modifier.</returns>
412         /// <since_tizen> 3 </since_tizen>
413         public bool IsAltModifier()
414         {
415             bool ret = Interop.Key.Key_IsAltModifier(swigCPtr);
416             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
417             return ret;
418         }
419
420         internal static global::System.Runtime.InteropServices.HandleRef getCPtr(Key obj)
421         {
422             return (obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.swigCPtr;
423         }
424
425         internal static Key GetKeyFromPtr(global::System.IntPtr cPtr)
426         {
427             Key ret = new Key(cPtr, false);
428             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
429             return ret;
430         }
431
432         /// <summary>
433         /// Dispose.
434         /// </summary>
435         /// <param name="type">The dispose type.</param>
436         /// <since_tizen> 3 </since_tizen>
437         protected virtual void Dispose(DisposeTypes type)
438         {
439             if (disposed)
440             {
441                 return;
442             }
443
444             if(type == DisposeTypes.Explicit)
445             {
446                 //Called by User
447                 //Release your own managed resources here.
448                 //You should release all of your own disposable objects here.
449             }
450
451             //Release your own unmanaged resources here.
452             //You should not access any managed member here except static instance.
453             //because the execution order of Finalizes is non-deterministic.
454
455             if (swigCPtr.Handle != global::System.IntPtr.Zero)
456             {
457                 if (swigCMemOwn)
458                 {
459                     swigCMemOwn = false;
460                     Interop.Key.delete_Key(swigCPtr);
461                 }
462                 swigCPtr = new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero);
463             }
464             disposed = true;
465         }
466     }
467 }