当前位置:Linux教程 - Linux - Linux程式设计入门 - termios/keymap/terminal progra

Linux程式设计入门 - termios/keymap/terminal progra

 termios

inttcgetattr(intfd,structtermios*termios_p);
inttcsetattr(intfd,intoptional_actions,conststructtermios
*termios_p);

keymap

我写了一个小程式来专门处理Linux上的keymap,keymap.h及keymap.c。在LinuxTerminal上,如果您想要设定某些按键返回特定值,您会需要用到以下这些技巧。

设定keymap

#include<linux/kd.h>
#include<linux/keyboard.h>
voidsetkeymap(void)

{

structkbentryKEYMAP;

KEYMAP.kb_table=STATE;

KEYMAP.kb_index=SCANCODE;

KEYMAP.kb_value=VALUE;

ioctl(console,KDSKBENT,&KEYMAP);

}


STATE为状态键组合

/usr/include/linux/keyboard.h中


#defineKG_SHIFT0

#defineKG_CTRL2

#defineKG_ALT3

#defineKG_ALTGR1

#defineKG_SHIFTL4

#defineKG_SHIFTR5

#defineKG_CTRLL6

#defineKG_CTRLR7

#defineKG_CAPSSHIFT8


使用方式如:

#defineKST_CTRL(1<<KG_CTRL)

#defineKST_ALT(1<<KG_ALT)

#defineKST_SHIFT(1<<KG_SHIFT)

#defineKST_CTRL_ALT(KST_CTRL|KST_ALT)

#defineKST_ALT_SHIFT(KST_ALT|KST_SHIFT)


SCANCODE为键盘扫描码


#defineSCAN_ESC0x01

#defineSCAN_10x02

#defineSCAN_20x03

#defineSCAN_30x04

#defineSCAN_40x05

#defineSCAN_50x06

#defineSCAN_60x07

#defineSCAN_70x08

#defineSCAN_80x09

#defineSCAN_90x0A

#defineSCAN_00x0B

#defineSCAN_MINUS0x0C

#defineSCAN_PLUS0x0D

#defineSCAN_BACK0x0E

#defineSCAN_TAB0x0F

#defineSCAN_Q0x10

#defineSCAN_W0x11

#defineSCAN_E0x12

#defineSCAN_R0x13

#defineSCAN_T0x14

#defineSCAN_Y0x15

#defineSCAN_U0x16

#defineSCAN_I0x17

#defineSCAN_O0x18

#defineSCAN_P0x19

#defineSCAN_LTQUOTE0x1A

#defineSCAN_RTQUOTE0x1B

#defineSCAN_ENTER0x1C

#defineSCAN_CTRL0x1D

#defineSCAN_A0x1E

#defineSCAN_S0x1F

#defineSCAN_D0x20

#defineSCAN_F0x21

#defineSCAN_G0x22

#defineSCAN_H0x23

#defineSCAN_J0x24

#defineSCAN_K0x25

#defineSCAN_L0x26

#defineSCAN_SPLIT0x27

#defineSCAN_QUOTE0x28

#defineSCAN_MARK0x29

#defineSCAN_LSHIFT0x2A

#defineSCAN_STAND0x2B

#defineSCAN_Z0x2C

#defineSCAN_X0x2D

#defineSCAN_C0x2E

#defineSCAN_V0x2F

#defineSCAN_B0x30

#defineSCAN_N0x31

#defineSCAN_M0x32

#defineSCAN_LSQUOTE0x33

#defineSCAN_RSQUOTE0x34

#defineSCAN_QUESTION0x35

#defineSCAN_RSHIFT0x36

#defineSCAN_PRTSCR0x37

#defineSCAN_ALT0x38

#defineSCAN_SPACE0x39

#defineSCAN_CAPSLOCK0x3A

#defineSCAN_F10x3B

#defineSCAN_F20x3C

#defineSCAN_F30x3D

#defineSCAN_F40x3E

#defineSCAN_F50x3F

#defineSCAN_F60x40

#defineSCAN_F70x41

#defineSCAN_F80x42

#defineSCAN_F90x43

#defineSCAN_F100x44

#defineSCAN_NUMLOCK0x45


#defineSCAN_HOME0x47

#defineSCAN_UP0x48

#defineSCAN_PGUP0x49

#defineSCAN_LEFT0x4B


#defineSCAN_RIGHT0x4D


#defineSCAN_END0x4F

#defineSCAN_DOWN0x50

#defineSCAN_PGDN0x51

#defineSCAN_INSERT0x52

#defineSCAN_DELETE0x53

#defineSCAN_F110x85

#defineSCAN_F120x86


/usr/include/linux/kd.h中


structkbentry{

unsignedcharkb_table;

unsignedcharkb_index;

unsignedshortkb_value;

};


#defineKDGKBENT0x4B46/*getsoneentryintranslation

table*/

#defineKDSKBENT0x4B47/*setsoneentryintranslation

table*/


而console为

console=open(""/dev/console"",O_RDWR);


读取按键


read(console,&c,sizeof(char));



terminalprogramming


term.h/term.c是我写来专门处理一些小型的互动界面程式。


Terminal指令集


设定颜色:〔colorm

其中color可以是以下的值


0:ResetColorAttributes

1:boldon

2:boldoff

4:underlineon

5:blinkon

7:reverseon

21/22:boldnormal

24:underlineoff

25:blinkoff

27:reverseoff


30:前景,黑色

31:前景,红色

32:前景,绿色

33:前景,黄色

34:前景,篮色

35:前景,紫色

36:前景,青色

37:前景,白色

40:背景,黑色

41:背景,红色

42:背景,绿色

43:背景,黄色

44:背景,篮色

45:背景,紫色

46:背景,青色

47:背景,白色


清除萤幕:c


设定水平游标位置:〔XG

X为水平游标位置。


设定垂直游标位置:〔Xd

Y为垂直游标位置。


〔YA

Current_Cursor_Y-=Y


〔YB或〔Ye

Current_Cursor_Y+=Y


〔XC或〔Xa

Current_Cursor_X+=X


〔XD

Current_Cursor_X-=X


〔YE

gotoxy(0,Current_Cursor_Y+Y)


〔YF

gotoxy(0,Current_Cursor_Y-Y)


〔Y;XH

gotoxy(X,Y);


〔0K:删除从游标到该行结尾

〔1K:删除从该行开始到游标处

〔2K:删除整行


〔0J:删除游标到萤幕结尾

〔1J:删除从萤幕开始到游标处

〔2J:删除整个萤幕


〔N@:insertNchar

〔P:deletechar

〔M:deleteline

〔L:insertline

〔s:savecursorposition

〔u:restorecursorposition


E:carry;linefeed

M:Current_Cursor_Y-1

D:linefeed