1 /*
2  * Software License Agreement (BSD License)
3  *
4  *  Point Cloud Library (PCL) - www.pointclouds.org
5  *  Copyright (c) 2010-2011, Willow Garage, Inc.
6  *  Copyright (c) 2012-, Open Perception, Inc.
7  *
8  *  All rights reserved.
9  *
10  *  Redistribution and use in source and binary forms, with or without
11  *  modification, are permitted provided that the following conditions
12  *  are met:
13  *
14  *   * Redistributions of source code must retain the above copyright
15  *     notice, this list of conditions and the following disclaimer.
16  *   * Redistributions in binary form must reproduce the above
17  *     copyright notice, this list of conditions and the following
18  *     disclaimer in the documentation and/or other materials provided
19  *     with the distribution.
20  *   * Neither the name of the copyright holder(s) nor the names of its
21  *     contributors may be used to endorse or promote products derived
22  *     from this software without specific prior written permission.
23  *
24  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32  *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34  *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35  *  POSSIBILITY OF SUCH DAMAGE.
36  *
37  */
38 
39 #pragma once
40 
41 #include <string>
42 
43 namespace pcl
44 {
45   namespace visualization
46   {
47     /** /brief Class representing key hit/release events */
48     class KeyboardEvent
49     {
50       public:
51         /** \brief bit patter for the ALT key*/
52         static const unsigned int Alt   = 1;
53         /** \brief bit patter for the Control key*/
54         static const unsigned int Ctrl  = 2;
55         /** \brief bit patter for the Shift key*/
56         static const unsigned int Shift = 4;
57 
58         /** \brief Constructor
59           * \param[in] action    true for key was pressed, false for released
60           * \param[in] key_sym   the key-name that caused the action
61           * \param[in] key       the key code that caused the action
62           * \param[in] alt       whether the alt key was pressed at the time where this event was triggered
63           * \param[in] ctrl      whether the ctrl was pressed at the time where this event was triggered
64           * \param[in] shift     whether the shift was pressed at the time where this event was triggered
65           */
66         inline KeyboardEvent (bool action, const std::string& key_sym, unsigned char key,
67                               bool alt, bool ctrl, bool shift);
68 
69         /**
70           * \return   whether the alt key was pressed at the time where this event was triggered
71           */
72         inline bool
73         isAltPressed () const;
74 
75         /**
76           * \return whether the ctrl was pressed at the time where this event was triggered
77           */
78         inline bool
79         isCtrlPressed () const;
80 
81         /**
82           * \return whether the shift was pressed at the time where this event was triggered
83           */
84         inline bool
85         isShiftPressed () const;
86 
87         /**
88           * \return the ASCII Code of the key that caused the event. If 0, then it was a special key, like ALT, F1, F2,... PgUp etc. Then the name of the key is in the keysym field.
89           */
90         inline unsigned char
91         getKeyCode () const;
92 
93         /**
94           * \return name of the key that caused the event
95           */
96         inline const std::string&
97         getKeySym () const;
98 
99         /**
100           * \return true if a key-press caused the event, false otherwise
101           */
102         inline bool
103         keyDown () const;
104 
105         /**
106           * \return true if a key-release caused the event, false otherwise
107           */
108         inline bool
109         keyUp () const;
110 
111       protected:
112 
113         bool action_;
114         unsigned int modifiers_;
115         unsigned char key_code_;
116         std::string key_sym_;
117     };
118 
KeyboardEvent(bool action,const std::string & key_sym,unsigned char key,bool alt,bool ctrl,bool shift)119     KeyboardEvent::KeyboardEvent (bool action, const std::string& key_sym, unsigned char key,
120                                   bool alt, bool ctrl, bool shift)
121       : action_ (action)
122       , modifiers_ (0)
123       , key_code_(key)
124       , key_sym_ (key_sym)
125     {
126       if (alt)
127         modifiers_ = Alt;
128 
129       if (ctrl)
130         modifiers_ |= Ctrl;
131 
132       if (shift)
133         modifiers_ |= Shift;
134     }
135 
136     bool
isAltPressed()137     KeyboardEvent::isAltPressed () const
138     {
139       return (modifiers_ & Alt) != 0;
140     }
141 
142     bool
isCtrlPressed()143     KeyboardEvent::isCtrlPressed () const
144     {
145       return (modifiers_ & Ctrl) != 0;
146     }
147 
148     bool
isShiftPressed()149     KeyboardEvent::isShiftPressed () const
150     {
151       return (modifiers_ & Shift) != 0;
152     }
153 
154     unsigned char
getKeyCode()155     KeyboardEvent::getKeyCode () const
156     {
157       return (key_code_);
158     }
159 
160     const std::string&
getKeySym()161     KeyboardEvent::getKeySym () const
162     {
163       return (key_sym_);
164     }
165 
166     bool
keyDown()167     KeyboardEvent::keyDown () const
168     {
169       return (action_);
170     }
171 
172     bool
keyUp()173     KeyboardEvent::keyUp () const
174     {
175       return (!action_);
176     }
177   } // namespace visualization
178 } // namespace pcl
179