Detecting keyboard state using c++ in ubuntu

801 views
Skip to first unread message

ash22194

unread,
Dec 14, 2014, 6:44:39 AM12/14/14
to wncc...@googlegroups.com
Is there a function in ubuntu similar to GetKeyboardState() in windows? I want to detect multiple keypresses in the terminal(one would encounter such a situation in a game where you use arrow keys to control and a combination of two keys would result in a diagonal walk).
Is there a way to do it in ubuntu using c++?
Thanks in advance.

Dilawar Singh

unread,
Dec 14, 2014, 1:01:42 PM12/14/14
to wncc_iitb
In terminal if you type `xev` then you can print key-events onto the console when you press the key. Its part of Xlib. You should be able to do it using this library.

best,
     Dilawar
     NCBS Bangalore

--
--
The website for the club is http://wncc-iitb.org/
To post to this group, send email to wncc...@googlegroups.com
---
You received this message because you are subscribed to the Google Groups "Web and Coding Club IIT Bombay" group.
To unsubscribe from this group and stop receiving emails from it, send an email to wncc_iitb+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


--
Dilawar
NCBS Bangalore

Aman Madaan

unread,
Dec 18, 2014, 1:49:24 AM12/18/14
to wncc...@googlegroups.com
You probably know this by now, but SDL can be used to do this without having to write code at the X11 level, which is something you probably shouldn't be doing for something as standard as this. (besides, x11 is not very friendly for beginners, and may cause a lot of head banging, spaghetti and heisenbugs, much like MFC/.net/VB vs win32 in the Windows parlance).

To set up SDL on ubuntu, you just need to apt-get install libsdl2-dev and then compile using g++  -I /usr/include/SDL2/  your_code.cpp -lSDL2

I have created a small example : https://gist.github.com/madaan/6f10b3539274ef75c952

It is based on http://www.programmersranch.com/2014/02/sdl2-keyboard-and-mouse-movement-events.html and allows you to move the character in any direction by pressing a combination of the keys (right + up for NE etc.)

cheers
-Aman
-Aman

ash22194

unread,
Dec 26, 2014, 3:16:54 AM12/26/14
to wncc...@googlegroups.com
@Aman I tried using SDL and used your code. But it gives many "not declared in this scope" errors. Do I need to include any other SDL library. I have installed SDL2.0.3 is it a problem of version mismatch?

Aman Madaan

unread,
Jan 1, 2015, 12:05:21 AM1/1/15
to wncc...@googlegroups.com
Please post all the errors (use something like pastebin.com). Also, how do you compile the code?
I am using SDL 2.0.2, it might be a compatibility issue.

Pratyush Nalam

unread,
Jan 1, 2015, 3:05:35 AM1/1/15
to wncc...@googlegroups.com

Pastebin has been blocked by the Government of India. Might stop working soon in IIT too I guess.

 

Pratyush Nalam

http://www.pratyushnalam.me

Reply all
Reply to author
Forward
0 new messages