#include <linux/kd.h> #include <sys/ioctl.h> #include <stdio.h> #include <errno.h> #define NUMLOCK 0x02 #define CAPS 0X04 #define SCROLL 0X01 int main(int argc,char **argv) { int a=NUMLOCK; char b=0; if(argc==1) { printf("usage /n./a.out - /n or usage /n ./a.out +/n"); return -1; } ioctl(0,KDGKBLED,&a);//读取小键盘状态 ioctl(0,KDGETLED,&b);//读取键盘灯状态 if(argv[1][0]=='-') { a &= ~NUMLOCK; a &=~CAPS; a &=~SCROLL; b &=~NUMLOCK; b &=~CAPS; b &=~SCROLL; } else { a |=NUMLOCK; a |=CAPS; a |=SCROLL; b |=NUMLOCK; b |=CAPS; b |=SCROLL; } ioctl(0,KDSKBLED,a);//设置键盘状态 ioctl(0,KDSETLED,b);//设置键盘灯状态 } 只能在控制台模式下控制键盘灯及小键盘状态 gcc test.c -o test -------------------------------------------------------------------------------------------- #define PACKAGE "Xnumlock" #define VERSION "1.0" #include <string.h> #include <stdlib.h> #include <stdio.h> #include <X11/Xlib.h> #include <X11/extensions/XTest.h> #include <X11/keysym.h> #include <errno.h> void usage( const char* argv0 ) { printf( "NumLockX " VERSION "/n" "Usage : %s [on|off]/n" "on - turns NumLock on in X ( default )/n" "off - turns NumLock off in X/n" "/n" , argv0 ); } Display* dpy; int xtest_get_state(int key) { int i; int numlock_mask = 0; Window dummy1, dummy2; int dummy3, dummy4, dummy5, dummy6; unsigned int mask; XModifierKeymap* map = XGetModifierMapping( dpy ); KeyCode numlock_keycode = XKeysymToKeycode( dpy, key ); if( numlock_keycode == NoSymbol ) return 0; for( i = 0;i < 8;++i ) { if( map->modifiermap[ map->max_keypermod * i ] == numlock_keycode ) numlock_mask = 1 << i; } XQueryPointer( dpy, DefaultRootWindow( dpy ), &dummy1, &dummy2,&dummy3, &dummy4, &dummy5, &dummy6, &mask ); XFreeModifiermap( map ); return mask & numlock_mask; } void xtest_change_on(int key) { errno=0; XTestFakeKeyEvent( dpy, XKeysymToKeycode( dpy, key ), True, CurrentTime ); perror("on"); } void xtest_change_off(int key) { errno=0; XTestFakeKeyEvent( dpy, XKeysymToKeycode( dpy, key ), False, CurrentTime ); perror("off"); } void set_on(int key) { if( !xtest_get_state(key)) xtest_change_on(key); } void set_off(int key) { if( xtest_get_state(key)) xtest_change_off(key); } int main( int argc, char* argv[] ) { /*if( argc > 2 ) { usage( argv[ 0 ] ); return 1; }*/ int st; dpy = XOpenDisplay( NULL ); if( dpy == NULL ) { fprintf( stderr, "Error opening display!/n" ); return 1; } while(1) { //scanf("%d",&st); //printf("sd =%d/n",st); sleep(3); if(st==1) { printf("set_on/n"); set_on(XK_Num_Lock); set_off(XK_Caps_Lock); st=0; } else { st=1; printf("set_off/n"); set_off(XK_Num_Lock); set_off(XK_Num_Lock); set_off(XK_Num_Lock); set_off(XK_Caps_Lock); } XCloseDisplay( dpy ); return 0; } 在x11模式下控制小键盘状态及键盘灯(这个是转的别人的,试验了下,效果不太理想,set_off和set_on需要连续调用两三次才生效) gcc -o Xtest test.c -lX11 -L/usr/X11R6/lib -lXext -lXtst ---------------------------------------------------------------------------------------------- 仅仅控制键盘灯的,即可以在控制台模式,也可在x11模式 int num=2,scr=1,caps=4; #define LED_CODE 0xED /* command to keyboard to set LEDs */ #define KEYBD 0x60 //argc[1] 1 2 4 3 5 6 7 int main(int argc,char **argv) { unsigned int leds; ioperm(KEYBD,8,1); //while(1) //printf("%d %d/n",usleep(100000),inb(KEYBD)); //读键盘扫描码 if(argc==1)return; leds=(scr<<0)|(num<<1)|(caps<<2); leds=atoi(argv[1]); outb(LED_CODE, KEYBD); outb(leds, KEYBD);//leds的第1,2,3位的值分别控制SCROll,NUM,CAPS }