1 // Copyright 2010-2018, Google Inc.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 //     * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 //     * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
13 // distribution.
14 //     * Neither the name of Google Inc. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 
30 // Utilities for commands::KeyEvent.
31 
32 #ifndef MOZC_COMPOSER_KEY_EVENT_UTIL_H_
33 #define MOZC_COMPOSER_KEY_EVENT_UTIL_H_
34 
35 #include "base/port.h"
36 
37 namespace mozc {
38 namespace commands {
39 class KeyEvent;
40 }  // namespace commands
41 
42 typedef uint64 KeyInformation;
43 
44 // This is pure static class.
45 class KeyEventUtil {
46  public:
47   static uint32 GetModifiers(const commands::KeyEvent &key_event);
48 
49   // |Modifiers(16bit)|SpecialKey(16bit)|Unicode(32bit)|
50   static bool GetKeyInformation(const commands::KeyEvent &key_event,
51                                 KeyInformation *key);
52 
53   // Normalizes given key event for key command looking-up. This function does
54   // - remove commands::KeyEvent::CAPS from the modifier keys
55   // - revert the flip of alphabetical key code caused by CapsLock
56   // so that shortcut keys can be used as if CapsLock was not enabled. b/5627459
57   // In addition, this function removes left / right specified modifiers.
58   static void NormalizeModifiers(const commands::KeyEvent &key_event,
59                                  commands::KeyEvent *new_key_event);
60 
61   // Normalizes a numpad key to a normal key (e.g. NUMPAD0 => '0')
62   static void NormalizeNumpadKey(const commands::KeyEvent &key_event,
63                                  commands::KeyEvent *new_key_event);
64 
65   // Removes modifier keys which are specified by |remove_modifiers|.
66   static void RemoveModifiers(const commands::KeyEvent &key_event,
67                               uint32 remove_modifiers,
68                               commands::KeyEvent *new_key_event);
69 
70   // Returns a fallback keyevent generated from key_event. In the
71   // current implementation, if the input key_event does not contains
72   // any special keys or modifier keys, that printable key will be replaced
73   // with the ASCII special key.
74   static bool MaybeGetKeyStub(const commands::KeyEvent &key_event,
75                               KeyInformation *key);
76 
77   static bool HasAlt(uint32 modifiers);
78   static bool HasCtrl(uint32 modifiers);
79   static bool HasShift(uint32 modifiers);
80   static bool HasCaps(uint32 modifiers);
81 
82   // These functions doesn't consider CAPS.
83   static bool IsAlt(uint32 modifiers);
84   static bool IsCtrl(uint32 modifiers);
85   static bool IsShift(uint32 modifiers);
86   static bool IsAltCtrl(uint32 modifiers);
87   static bool IsAltShift(uint32 modifiers);
88   static bool IsCtrlShift(uint32 modifiers);
89   static bool IsAltCtrlShift(uint32 modifiers);
90 
91   static bool IsLowerAlphabet(const commands::KeyEvent &key_event);
92   static bool IsUpperAlphabet(const commands::KeyEvent &key_event);
93   static bool IsNumpadKey(const commands::KeyEvent &key_event);
94 
95  private:
96   DISALLOW_IMPLICIT_CONSTRUCTORS(KeyEventUtil);
97 };
98 
99 }  // namespace mozc
100 
101 #endif  // MOZC_COMPOSER_KEY_EVENT_UTIL_H_
102