📄 libkbd.c
字号:
/*
* This source code is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* File Name: libkbd.c
*
* Reference:
*
* Author: Li Feng
*
* Description:
*
*
*
* History:
* 02/23/2005 Li Feng Created
*
*
*CodeReview Log:
*
*/
#include <sys/types.h>#include <fcntl.h>#include <unistd.h>#include <errno.h>#include <termios.h>#include <fcntl.h>#include <sys/ioctl.h>#include <sys/stat.h>#include <linux/kd.h>
#include <stdlib.h>
#include "btype.h"
#include "libkbd.h"
typedef struct tagKBD_Param
{
int kbd_fd; /* file descriptor for keyboard */
struct termios startup_termios; /* original terminal modes */
int startup_kbdmode;
struct termios work_termios; /* startup_termios terminal modes */
}KBD_Param;/* * Open the keyboard. * This is real simple, we just use a special file handle * that allows blocking I/O, and put the terminal into * character mode. */HKBD KBD_Open (char *pDev){ KBD_Param *pKBD;
pKBD = (KBD_Param *)malloc(sizeof(KBD_Param));
if(pKBD==NULL)
return NULL;
pKBD->kbd_fd = open(pDev, O_RDONLY | O_NOCTTY); if (pKBD->kbd_fd < 0)
{
free(pKBD);
return NULL;
} if (tcgetattr(pKBD->kbd_fd, &(pKBD->startup_termios)) < 0)
{
close(pKBD->kbd_fd);
free(pKBD);
return NULL;
} pKBD->work_termios = pKBD->startup_termios; pKBD->work_termios.c_lflag &= ~(ICANON | ECHO | ISIG); pKBD->work_termios.c_iflag &= ~(ISTRIP | IGNCR | ICRNL | INLCR | IXOFF | IXON); pKBD->work_termios.c_iflag |= IGNBRK; pKBD->work_termios.c_cc[VMIN] = 0; pKBD->work_termios.c_cc[VTIME] = 0; if(tcsetattr(pKBD->kbd_fd, TCSAFLUSH, &pKBD->work_termios) < 0)
{
close(pKBD->kbd_fd);
free(pKBD);
return NULL;
} /* Put the keyboard into MEDIUMRAW mode. Despite the name, this * is really "mostly raw", with the kernel just folding long * scancode sequences (e.g. E0 XX) onto single keycodes. */ ioctl(pKBD->kbd_fd, KDGKBMODE, &pKBD->startup_kbdmode); if (ioctl(pKBD->kbd_fd, KDSKBMODE, K_MEDIUMRAW) < 0)
{
close(pKBD->kbd_fd);
free(pKBD);
return NULL;
} return (HKBD)pKBD;}/* * Close the keyboard. * This resets the terminal modes. */void KBD_Close(HKBD hKBD){
KBD_Param *pKBD = (KBD_Param *)hKBD;
if(pKBD==NULL)
return; ioctl(pKBD->kbd_fd, KDSKBMODE, pKBD->startup_kbdmode); tcsetattr(pKBD->kbd_fd, TCSANOW, &pKBD->startup_termios); close(pKBD->kbd_fd);
free(pKBD);}/* * This reads one keystroke from the keyboard, and the current state of * the mode keys (ALT, SHIFT, CTRL). Returns -1 on error, 0 if no data * is ready, and 1 if data was read. This is a non-blocking call. */BOOL KBD_Read(HKBD hKBD, UINT8 *buf){ int cc; /* characters read */ unsigned char buf1; KBD_Param *pKBD = (KBD_Param *)hKBD;
if(pKBD==NULL)
return FALSE;
do
{
cc = read(pKBD->kbd_fd, &buf1, 1);
}while(cc==0);
*buf = buf1; if (cc > 0)
{ return TRUE; } if ((cc < 0) && (errno != EINTR) && (errno != EAGAIN)) return FALSE;
return TRUE;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -