Linux程序设计入门 - termios/keymap/terminal programming
int tcgetattr (int fd, struct termios *termios_p);
int tcsetattr (int fd, int optional_actions,const struct termios
*termios_p);
keymap
我写了一个小程序来专门处理Linux上的keymap,keymap.h及keymap.c。
在Linux Terminal上,如果您想要设定某些按键返回特定值,您会需要用到以
下这些技巧。
设定keymap
#include <linux/kd.h>
#include <linux/keyboard.h>
void setkeymap(void)
{
struct kbentry KEYMAP;
KEYMAP.kb_table=STATE;
KEYMAP.kb_index=SCANCODE;
KEYMAP.kb_value=VALUE;
ioctl(console,KDSKBENT,&KEYMAP);
}
STATE为状态键组合
/usr/include/linux/keyboard.h中
#define KG_SHIFT 0
#define KG_CTRL 2
#define KG_ALT 3
#define KG_ALTGR 1
#define KG_SHIFTL 4
#define KG_SHIFTR 5
#define KG_CTRLL 6
#define KG_CTRLR 7
#define KG_CAPSSHIFT 8
使用方式如:
#define KST_CTRL (1<<KG_CTRL)
#define KST_ALT (1<<KG_ALT)
#define KST_SHIFT (1<<KG_SHIFT)
#define KST_CTRL_ALT (KST_CTRL|KST_ALT)
#define KST_ALT_SHIFT (KST_ALT|KST_SHIFT)
SCANCODE为键盘扫描码
#define SCAN_ESC 0x01
#define SCAN_1 0x02
#define SCAN_2 0x03
#define SCAN_3 0x04
#define SCAN_4 0x05
#define SCAN_5 0x06
#define SCAN_6 0x07
#define SCAN_7 0x08
#define SCAN_8 0x09
#define SCAN_9 0x0A
#define SCAN_0 0x0B
#define SCAN_MINUS 0x0C
#define SCAN_PLUS 0x0D
#define SCAN_BACK 0x0E
#define SCAN_TAB 0x0F
#define SCAN_Q 0x10
#define SCAN_W 0x11
#define SCAN_E 0x12
#define SCAN_R 0x13
#define SCAN_T 0x14
#define SCAN_Y 0x15
#define SCAN_U 0x16
#define SCAN_I 0x17
#define SCAN_O 0x18
#define SCAN_P 0x19
#define SCAN_LTQUOTE 0x1A
#define SCAN_RTQUOTE 0x1B
#define SCAN_ENTER 0x1C
#define SCAN_CTRL 0x1D
#define SCAN_A 0x1E
#define SCAN_S 0x1F
#define SCAN_D 0x20
#define SCAN_F 0x21
#define SCAN_G 0x22
#define SCAN_H 0x23
#define SCAN_J 0x24
#define SCAN_K 0x25
#define SCAN_L 0x26
#define SCAN_SPLIT 0x27
#define SCAN_QUOTE 0x28
#define SCAN_MARK 0x29
#define SCAN_LSHIFT 0x2A
#define SCAN_STAND 0x2B
#define SCAN_Z 0x2C
#define SCAN_X 0x2D
#define SCAN_C 0x2E
#define SCAN_V 0x2F
#define SCAN_B 0x30
#define SCAN_N 0x31
#define SCAN_M 0x32
#define SCAN_LSQUOTE 0x33
#define SCAN_RSQUOTE 0x34
#define SCAN_QUESTION 0x35
#define SCAN_RSHIFT 0x36
#define SCAN_PRTSCR 0x37
#define SCAN_ALT 0x38
#define SCAN_SPACE 0x39
#define SCAN_CAPSLOCK 0x3A
#define SCAN_F1 0x3B
#define SCAN_F2 0x3C
#define SCAN_F3 0x3D
#define SCAN_F4 0x3E
#define SCAN_F5 0x3F
#define SCAN_F6 0x40
#define SCAN_F7 0x41
#define SCAN_F8 0x42
#define SCAN_F9 0x43
#define SCAN_F10 0x44
#define SCAN_NUMLOCK 0x45
#define SCAN_HOME 0x47
#define SCAN_UP 0x48
#define SCAN_PGUP 0x49
#define SCAN_LEFT 0x4B
#define SCAN_RIGHT 0x4D
#define SCAN_END 0x4F
#define SCAN_DOWN 0x50
#define SCAN_PGDN 0x51
#define SCAN_INSERT 0x52
#define SCAN_DELETE 0x53
#define SCAN_F11 0x85
#define SCAN_F12 0x86
/usr/include/linux/kd.h中
struct kbentry {
unsigned char kb_table;
unsigned char kb_index;
unsigned short kb_value;
};
#define KDGKBENT 0x4B46 /* gets one entry in translation
table */
#define KDSKBENT 0x4B47 /* sets one entry in translation
table */
而console为
console = open("/dev/console",O_RDWR);
读取按键
read(console,&c,sizeof(char));
terminal programming
term.h/term.c是我写来专门处理一些小型的互动界面程序。
Terminal指令集
设定颜色 : \033[colorm
其中color可以是以下的值
0 : Reset Color Attributes
1 : bold on
2 : bold off
4 : underline on
5 : blink on
7 : reverse on
21/22 : bold normal
24 : underline off
25 : blink off
27 : reverse off
30 : 前景,黑色
31 : 前景,红色
32 : 前景,绿色
33 : 前景,黄色
34 : 前景,篮色
35 : 前景,紫色
36 : 前景,青色
37 : 前景,白色
40 : 背景,黑色
41 : 背景,红色
42 : 背景,绿色
43 : 背景,黄色
44 : 背景,篮色
45 : 背景,紫色
46 : 背景,青色
47 : 背景,白色
清除萤幕 : \033c
设定水平游标位置 : \033[XG
X为水平游标位置。
设定垂直游标位置 : \033[Xd
Y为垂直游标位置。
\033[YA
Current_Cursor_Y -= Y
\033[YB或\033[Ye
Current_Cursor_Y += Y
\033[XC或\033[Xa
Current_Cursor_X += X
\033[XD
Current_Cursor_X -= X
\033[YE
gotoxy(0,Current_Cursor_Y+Y)
\033[YF
gotoxy(0,Current_Cursor_Y-Y)
\033[Y;XH
gotoxy(X,Y);
\033[0K : 删除从游标到该行结尾
\033[1K : 删除从该行开始到游标处
\033[2K : 删除整行
\033[0J : 删除游标到萤幕结尾
\033[1J : 删除从萤幕开始到游标处
\033[2J : 删除整个萤幕
\033[N@ : insert N char
\033[P : delete char
\033[M : delete line
\033[L : insert line
\033[s : save cursor position
\033[u : restore cursor position
\033E : carry ; linefeed
\033M : Current_Cursor_Y-1
\033D : linefeed
OK STATION, Webmaster, Brian Lin