如何在Linux上以编程方式打开/关闭Caps Lock,Scroll Lock和Num Lock键 [英] How can I turn on/off Caps Lock, Scroll Lock, Num Lock key programatically on Linux

查看:283
本文介绍了如何在Linux上以编程方式打开/关闭Caps Lock,Scroll Lock和Num Lock键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用C ++的Linux(OpenSuse)上是否有一种简单的方法来打开/关闭Caps Lock,Scroll Lock和Num Lock,需要使用哪些头文件? 我想控制一些设备来模拟击键.

Is there a simple method to turn on/off Caps Lock, Scroll Lock and Num Lock on Linux (OpenSuse) using C++, what header files need to use? I want to control some device simulates keystrokes.

推荐答案

解决方案1 ​​

请继续前进,因为此解决方案只是打开键盘的LED,如果您还需要启用 caps lock 功能,请参阅解决方案2.

Solution 1

Please go head because this solution just turn on the led of the keyboard, if you need to enable the caps lock funcion too, see solution 2.

// Linux header, no portable source
#include <sys/ioctl.h>
#include <fcntl.h>
#include <unistd.h>

int main(int argc, char* argv[]) {
  int fd_console = open("/dev/console", O_WRONLY);
  if (fd_console == -1) {
    std::cerr << "Error opening console file descriptor\n";
    exit(-1);
  }

  // turn on caps lock
  ioctl(fd_console, 0x4B32, 0x04);

  // turn on num block 
  ioctl(fd_console, 0x4B32, 0x02);

  // turn off 
  ioctl(fd_console, 0x4B32, 0x0);

  close(fd_console);
  return 0;
}

请记住,您必须以超级用户权限启动程序才能写入文件/dev/console.

Remember you have to launch your program with superuser privileges in order to write in the file /dev/console.

此解决方案可与X11窗口系统管理器一起使用(在Linux上几乎是标准的).

This solution works with X11 window system manager (on linux is almost a standard).

// X11 library and testing extensions
#include <X11/Xlib.h>
#include <X11/keysym.h>
#include <X11/extensions/XTest.h>

int main(int argc, char *argv[]) {
  // Get the root display.
  Display* display = XOpenDisplay(NULL);

  // Get the keycode for XK_Caps_Lock keysymbol
  unsigned int keycode = XKeysymToKeycode(display, XK_Caps_Lock);

  // Simulate Press
  XTestFakeKeyEvent(display, keycode, True, CurrentTime);
  XFlush(display);

  // Simulate Release
  XTestFakeKeyEvent(display, keycode, False, CurrentTime);
  XFlush(display);

  return 0;
}

注意:更多的键符号可以在标题 .

这篇关于如何在Linux上以编程方式打开/关闭Caps Lock,Scroll Lock和Num Lock键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆