forked from cproape/toolkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tk_event_samples.c
97 lines (89 loc) · 2.94 KB
/
tk_event_samples.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/**
* 说明:
* 创建2个事件句柄
* 静态创建事件1,包含两个事件标志,分别为event1_flag1和event1_flag2,当按下'a'键发送event1_flag1
* 当按下'b'键发送event1_flag2,由于使用AND,只有按下'a'和'b'后,才会触发事件处理程序
* 动态创建事件2,包含两个事件标志,分别为event2_flag1和event2_event2,当按下'c'键发送event2_flag1
* 当按下'd'键发送event2_flag2,由于使用OR,任意按下'c'或者'd'后,都会触发事件处理程序
*
* 注意:
* 英文小写输入法
*
* Change Logs:
* Date Author Notes
* 2020-01-31 zhangran the first version
* 2023-04-17 shadow3d change the output format and file format
*/
#include <windows.h>
#include <stdio.h>
#include <conio.h>
#include "toolkit.h"
/* 事件1句柄 */
struct tk_event event1;
/* 事件1标志 */
#define event1_flag1 (1 << 1)
#define event1_flag2 (1 << 2)
/* 事件2句柄 */
struct tk_event *event2 = NULL;
/* 事件2标志 */
#define event2_flag1 (1 << 1)
#define event2_flag2 (1 << 2)
int main(int argc, char *argv[])
{
uint32_t recved;
/* 静态创建事件1 */
tk_event_init(&event1);
/* 动态创建事件2 */
event2 = tk_event_create();
while (1)
{
if (_kbhit())
{
/* 获取安按键 */
int key = _getch();
/* printf("the key you pressed is %d\n", key); */
switch (key)
{
/* 按键'a'*/
case 97:
tk_event_send(&event1, event1_flag1);
printf("Key 'a' pressed send event1_flag1\n");
break;
/* 按键'b'*/
case 98:
tk_event_send(&event1, event1_flag2);
printf("Key 'b' pressed send event1_flag2\n");
break;
/* 按键'c'*/
case 99:
tk_event_send(event2, event2_flag1);
printf("Key 'c' pressed send event2_flag1\n");
break;
/* 按键'd'*/
case 100:
tk_event_send(event2, event2_flag2);
printf("Key 'd' pressed send event2_flag2\n");
break;
}
}
/* 事件1同时接收到了标志1和标志2,清除标志并触发事件处理程序 */
if (true == tk_event_recv(&event1,
(event1_flag1 | event1_flag2),
TK_EVENT_OPTION_AND | TK_EVENT_OPTION_CLEAR,
&recved))
{
printf("trigger event and clear event\n");
printf("event1: AND recv event 0x%x\n\n", recved);
}
/* 事件2接收到了标志1或标志2,清除标志并触发事件处理程序 */
if (true == tk_event_recv(event2,
(event2_flag1 | event2_flag2),
TK_EVENT_OPTION_OR | TK_EVENT_OPTION_CLEAR,
&recved))
{
printf("trigger event and clear event\n");
printf("event2: OR recv event 0x%x\n\n", recved);
}
}
return 0;
}