librime: key_event.h Source File

| librime 1.2 Rime Input Method Engine, the core library

key_event.h
Go to the documentation of this file.
1// encoding: utf-8
2//
3// Copyright RIME Developers
4// Distributed under the BSD License
5//
6// 2011-04-17 GONG Chen <chen.sst@gmail.com>
7//
8#ifndef RIME_KEY_EVENT_H_
9#define RIME_KEY_EVENT_H_
10
11#include <iostream>
12#include <rime/common.h>
13#include <rime/key_table.h>
14
15namespace rime {
16
17 class KeyEvent {
18 public:
19 KeyEvent() = default;
20 KeyEvent(int keycode, int modifier)
21 : keycode_(keycode), modifier_(modifier) {}
22 RIME_DLL KeyEvent(const string& repr);
23
24 int keycode() const { return keycode_; }
25 void keycode(int value) { keycode_ = value; }
26 int modifier() const { return modifier_; }
27 void modifier(int value) { modifier_ = value; }
28
29 bool shift() const { return (modifier_ & kShiftMask) != 0; }
30 bool ctrl() const { return (modifier_ & kControlMask) != 0; }
31 bool alt() const { return (modifier_ & kAltMask) != 0; }
32 bool caps() const { return (modifier_ & kLockMask) != 0; }
33 bool super() const { return (modifier_ & kSuperMask) != 0; }
34 bool release() const { return (modifier_ & kReleaseMask) != 0; }
35 // 按鍵表示為形如「狀態+鍵名」的文字
36 // 若無鍵名，則以四位或六位十六进制数形式的文字來標識
37 // 形如 "0x12ab", "0xfffffe"
38 RIME_DLL string repr() const;
39
40 // 解析文字表示的按鍵
41 RIME_DLL bool Parse(const string& repr);
42
43 bool operator==(const KeyEvent& other) const {
44 return keycode_ == other.keycode_ && modifier_ == other.modifier_;
45 }
46
47 bool operator<(const KeyEvent& other) const {
48 if (keycode_ != other.keycode_)
49 return keycode_ < other.keycode_;
50 return modifier_ < other.modifier_;
51 }
52
53 private:
54 int keycode_ = 0;
55 int modifier_ = 0;
56};
57
58// 按鍵序列
59 class KeySequence : public vector<KeyEvent> {
60 public:
61 KeySequence() = default;
62 RIME_DLL KeySequence(const string& repr);
63
64 // 可表示為一串文字
65 // 若其中包含不產生可打印字符的按鍵，以 {鍵名} 來標記
66 // 組合鍵也用 {組合鍵狀態+鍵名} 來標記
67 RIME_DLL string repr() const;
68
69 // 解析按鍵序列描述文字
70 RIME_DLL bool Parse(const string& repr);
71};
72
73 inline std::ostream& operator<<(std::ostream& out, const KeyEvent& key_event) {
74 out << key_event.repr();
75 return out;
76}
77
78 inline std::ostream& operator<<(std::ostream& out, const KeySequence& key_seq) {
79 out << key_seq.repr();
80 return out;
81}
82
83} // namespace rime
84
85#endif // RIME_KEY_EVENT_H_
RIME_DLL
#define RIME_DLL
Definition rime_api.h:33
key_table.h
kControlMask
@ kControlMask
Definition key_table.h:17
kLockMask
@ kLockMask
Definition key_table.h:16
kSuperMask
@ kSuperMask
Definition key_table.h:39
kShiftMask
@ kShiftMask
Definition key_table.h:15
kAltMask
@ kAltMask
Definition key_table.h:19
kReleaseMask
@ kReleaseMask
Definition key_table.h:43
common.h
rime
Definition algebra.cc:12
rime::operator<<
std::ostream & operator<<(std::ostream &stream, const Reference &reference)
Definition config_compiler.cc:12
rime::KeyEvent
Definition key_event.h:17
rime::KeyEvent::release
bool release() const
Definition key_event.h:34
rime::KeyEvent::modifier
int modifier() const
Definition key_event.h:26
rime::KeyEvent::ctrl
bool ctrl() const
Definition key_event.h:30
rime::KeyEvent::repr
RIME_DLL string repr() const
Definition key_event.cc:19
rime::KeyEvent::KeyEvent
KeyEvent(int keycode, int modifier)
Definition key_event.h:20
rime::KeyEvent::keycode
int keycode() const
Definition key_event.h:24
rime::KeyEvent::alt
bool alt() const
Definition key_event.h:31
rime::KeyEvent::super
bool super() const
Definition key_event.h:33
rime::KeyEvent::operator==
bool operator==(const KeyEvent &other) const
Definition key_event.h:43
rime::KeyEvent::Parse
RIME_DLL bool Parse(const string &repr)
Definition key_event.cc:51
rime::KeyEvent::keycode
void keycode(int value)
Definition key_event.h:25
rime::KeyEvent::KeyEvent
KeyEvent()=default
rime::KeyEvent::modifier
void modifier(int value)
Definition key_event.h:27
rime::KeyEvent::caps
bool caps() const
Definition key_event.h:32
rime::KeyEvent::shift
bool shift() const
Definition key_event.h:29
rime::KeyEvent::operator<
bool operator<(const KeyEvent &other) const
Definition key_event.h:47
rime::KeySequence
Definition key_event.h:59
rime::KeySequence::Parse
RIME_DLL bool Parse(const string &repr)
Definition key_event.cc:111
rime::KeySequence::repr
RIME_DLL string repr() const
Definition key_event.cc:95
rime::KeySequence::KeySequence
KeySequence()=default