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
17class KeyEvent {
18 public:
19 KeyEvent() = default;
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// 按鍵序列
59class 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
73inline std::ostream& operator<<(std::ostream& out, const KeyEvent& key_event) {
74 out << key_event.repr();
75 return out;
76}
77
78inline 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_
@ kControlMask
Definition key_table.h:17
@ kLockMask
Definition key_table.h:16
@ kSuperMask
Definition key_table.h:39
@ kShiftMask
Definition key_table.h:15
@ kAltMask
Definition key_table.h:19
@ kReleaseMask
Definition key_table.h:43
#define RIME_DLL
Definition rime_api.h:33
Definition algebra.cc:12
std::ostream & operator<<(std::ostream &stream, const Reference &reference)
Definition config_compiler.cc:12
Definition key_event.h:17
bool release() const
Definition key_event.h:34
int modifier() const
Definition key_event.h:26
bool ctrl() const
Definition key_event.h:30
RIME_DLL string repr() const
Definition key_event.cc:19
KeyEvent(int keycode, int modifier)
Definition key_event.h:20
int keycode() const
Definition key_event.h:24
bool alt() const
Definition key_event.h:31
bool super() const
Definition key_event.h:33
bool operator==(const KeyEvent &other) const
Definition key_event.h:43
RIME_DLL bool Parse(const string &repr)
Definition key_event.cc:51
void keycode(int value)
Definition key_event.h:25
KeyEvent()=default
void modifier(int value)
Definition key_event.h:27
bool caps() const
Definition key_event.h:32
bool shift() const
Definition key_event.h:29
bool operator<(const KeyEvent &other) const
Definition key_event.h:47
Definition key_event.h:59
RIME_DLL bool Parse(const string &repr)
Definition key_event.cc:111
RIME_DLL string repr() const
Definition key_event.cc:95
KeySequence()=default