Linux程式設計入門 - termios/keymap/terminal program(轉)

subid發表於2007-08-17
Linux程式設計入門 - termios/keymap/terminal program(轉)[@more@] 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指令集


設定顏色:33〔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:背景,白色


清除螢幕:33c


設定水平遊標位置:33〔XG

X為水平遊標位置。


設定垂直遊標位置:33〔Xd

Y為垂直遊標位置。


33〔YA

Current_Cursor_Y-=Y


33〔YB或33〔Ye

Current_Cursor_Y+=Y


33〔XC或33〔Xa

Current_Cursor_X+=X


33〔XD

Current_Cursor_X-=X


33〔YE

gotoxy(0,Current_Cursor_Y+Y)


33〔YF

gotoxy(0,Current_Cursor_Y-Y)


33〔Y;XH

gotoxy(X,Y);


33〔0K:刪除從遊標到該行結尾

33〔1K:刪除從該行開始到遊標處

33〔2K:刪除整行


33〔0J:刪除遊標到螢幕結尾

33〔1J:刪除從螢幕開始到遊標處

33〔2J:刪除整個螢幕


33〔N@:insertNchar

33〔P:deletechar

33〔M:deleteline

33〔L:insertline

33〔s:savecursorposition

33〔u:restorecursorposition


33E:carry;linefeed

33M:Current_Cursor_Y-1

33D:linefeed

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10796304/viewspace-963411/,如需轉載,請註明出處,否則將追究法律責任。

相關文章