1 /*
2  * Copyright (C) 2004 Ivo Danihelka (ivo@danihelka.net)
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  */
9 #include "KeyStroke.h"
10 
11 #include "StringTool.h"
12 
13 //-----------------------------------------------------------------
14 /**
15  * Create new keystroke from event.
16  */
KeyStroke(const SDL_keysym & keysym)17 KeyStroke::KeyStroke(const SDL_keysym &keysym)
18 {
19     m_sym = keysym.sym;
20     m_mod = modStrip(keysym.mod);
21     m_unicode = keysym.unicode;
22 }
23 //-----------------------------------------------------------------
24 /**
25  * Create new keystroke.
26  * NOTE: KMOD_ALT mean (KMOD_LALT and KMOD_RALT),
27  * i.e. either ALTs pressed!
28  *
29  * @param sym SDLKey
30  * @param mod SDLMod ored
31  */
KeyStroke(SDLKey sym,int mod)32 KeyStroke::KeyStroke(SDLKey sym, int mod)
33 {
34     m_sym = sym;
35     m_mod = modStrip(mod);
36     m_unicode = 0;
37 }
38 //-----------------------------------------------------------------
39 /**
40  * Strip ignored modes.
41  * KMOD_SHIFT|KMOD_NUM|KMOD_CAPS|KMOD_MODE are ignored.
42  */
43     int
modStrip(int mod)44 KeyStroke::modStrip(int mod)
45 {
46     return mod & ~STROKE_IGNORE;
47 }
48 //-----------------------------------------------------------------
49 /**
50  * KeyStroke comparation.
51  *
52  * @param other other keystroke
53  * @return this < other
54  */
55 bool
less(const KeyStroke & other) const56 KeyStroke::less(const KeyStroke &other) const
57 {
58     bool result = m_sym < other.m_sym;
59     if (m_sym == other.m_sym) {
60         result = m_mod < other.m_mod;
61     }
62     return result;
63 }
64 //-----------------------------------------------------------------
65 /**
66  * Test keyStroke equality.
67  * KMOD_NUM|KMOD_CAPS|KMOD_MODE are ignored.
68  *
69  * @param other other keystroke
70  * @return this == other
71  */
72 bool
equals(const KeyStroke & other) const73 KeyStroke::equals(const KeyStroke &other) const
74 {
75     return m_sym == other.m_sym &&
76         m_mod == other.m_mod;
77 }
78 //-----------------------------------------------------------------
79 /**
80  * Return text fashion.
81  */
82 std::string
toString() const83 KeyStroke::toString() const
84 {
85     std::string result = SDL_GetKeyName(m_sym);
86     result.append("+" + StringTool::toString(m_mod));
87     return result;
88 }
89 
90