直接贴代码:
keyboardfun.c
/* * ===================================================================================== * * Filename: keyboardfun.c * * Description: * * Version: 1.0 * Created: 2011年10月30日 14时41分29秒 * Revision: none * Compiler: gcc * * Author: Andy Lee , [email protected] * Company: * License: Licensed under the Academic Free License version 2.1 * * ===================================================================================== */ #define ESC ((char) 0x1B) #include <geekos/screen.h> #include <geekos/keyboard.h> static void left(void); static void right(void); static void up(void); static void down(void); static void home(void); static void end(int); static void ascii_bs(void); /*下面这两个函数暂时没用,一个是保存光标的位置,一个是恢复光标位置*/ static __inline__ void save(void); static __inline__ void restore(void); void KeyboardFunc(void) { int numchar = 0; Keycode key; char getkey; while(true) { key = Wait_For_Key(); if( !(key & KEY_SPECIAL_FLAG) && !(key & KEY_KEYPAD_FLAG)) { if( key & KEY_CTRL_FLAG ) { if( (key & 0xff) == 'd' ) break; } else { getkey = key & 0xff; switch (getkey) { case ASCII_ESC: Print("ESC"); break; case ASCII_BS: ascii_bs(); break; case '\n': Print("%c", key); numchar = 0; break; default: Print("%c",key); numchar++; } } } else if((key & KEY_KEYPAD_FLAG) && (key & KEY_SPECIAL_FLAG)) { switch (key) { case KEY_KPHOME: home(); break; case KEY_KPUP: up(); break; case KEY_KPPGUP: Print("PGUP"); break; case KEY_KPMINUS: Print("MINUS"); break; case KEY_KPLEFT: left(); break; case KEY_KPCENTER: Print("CENTER"); break; case KEY_KPRIGHT: right(); break; case KEY_KPPLUS: Print("PLUS"); break; case KEY_KPEND: end(numchar); break; case KEY_KPDOWN: down(); break; case KEY_KPPGDN: Print("PGDN"); break; case KEY_KPINSERT: Print("INSERT"); break; case KEY_KPDEL: Print("DEL"); break; } } else if(!(key & KEY_KEYPAD_FLAG) && (key & KEY_SPECIAL_FLAG)) { switch (key) { case KEY_UNKNOWN: break; case KEY_F1: Print("F1"); break; case KEY_F2: Print("F2"); break; case KEY_F3: Print("F3"); break; case KEY_F4: Print("F4"); break; case KEY_F5: Print("F5"); break; case KEY_F6: Print("F6"); break; case KEY_F7: Print("F7"); break; case KEY_F8: Print("F8"); break; case KEY_F9: Print("F9"); break; case KEY_F10: Print("F10"); break; case KEY_F11: Print("F11"); break; case KEY_F12: Print("F12"); break; } } // Print("This is KeyboardFunc runingI\n"); } Print("\nEnd Input!\n"); } static void ascii_bs(void) { int row, col; Get_Cursor(&row, &col); Put_Cursor(row, col-1); Put_Char(ESC); Put_Char('['); Put_Char('K'); } static void left(void) { int row, col; Get_Cursor(&row, &col); Put_Cursor(row, col-1); } static void right(void) { int row, col; Get_Cursor(&row, &col); Put_Cursor(row, col+1); } static void up(void) { int row, col; Get_Cursor(&row, &col); Put_Cursor(row-1, col); } static void down(void) { int row, col; Get_Cursor(&row, &col); Put_Cursor(row+1, col); } static void home(void) { int row, col; Get_Cursor(&row, &col); Put_Cursor(row, 0); } static void end(int num) { int row, col; Get_Cursor(&row, &col); Put_Cursor(row, num); } static __inline__ void save(void) { Put_Char(ESC); Put_Char('['); Put_Char('s'); } static __inline__ void restore(void) { Put_Char(ESC); Put_Char('['); Put_Char('u'); }
/* * ===================================================================================== * * Filename: keyboardfun.h * * Description: * * Version: 1.0 * Created: 2011年10月30日 14时43分39秒 * Revision: none * Compiler: gcc * * Author: Andy Lee , [email protected] * Company: * License: Licensed under the Academic Free License version 2.1 * * ===================================================================================== */ #ifndef KEYBOARDFUNC_H #define KEYBOARDFUNC_H void KeyboardFunc(void); #endif
具体使用: