Detecting keydown and keyup events on Linux C++ [duplicate]

爱⌒轻易说出口 提交于 2019-12-01 11:36:01

You could read the raw device, I havn't tried this but this blog post looks promising: http://www.thelinuxdaily.com/2010/05/grab-raw-keyboard-input-from-event-device-node-devinputevent/

so essentially you're reading directly from /dev/input/*

You can verify this works by running sudo cat /dev/input/eventX where X is one of the event devices listed in that directory (one of them will be your keyboard.. I'm sure there is a good way of finding which one programatically, but you can quickly find out by looking in /dev/input/by-id/ or just reading from one of those symlinks directly.)


NOTE: This will get you keyboard input all the time, not just when your window is in focus.. (your program wouldn't even need to be running in an xterm, or even a pty for that matter).

Dirk Holsopple

Realistically, you aren't going to be able to do this without a library. If you want something that has very little overhead, I would suggest ncurses. If you absolutely must do it without a library, look at how ncurses implements it. It will, of course, be very complicated.

I would consider taking a look at OIS (Object Oriented Input System) library. It is quite easy to use, has a good OO design and it is cross-platform. For a tutorial and some code take a look at http://www.ogre3d.org/tikiwiki/tiki-index.php?page=Using+OIS.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!