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