need Help! get Key State (Linux)

Hi,
I want to get the Key State of the arrow keys in linux, because I'm trying to create something like a simple game.
I've been searching on Google for hours, but all code I found was for Windows ONLY.
For my program, I was going to use a loop like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
//...
#define Xmax 40
#define Ymax 40
#define Xmin 0
#define Ymin 0
main()
{
int movement=0;
int PlayerPosition[Xmax][Ymax];
int PlayerX, PlayerY;
int RY,RX;
for(RX=0;RX<Xmax;RX++)
   for(RY=0;RY<Ymax;RY++)
      PlayerPosition[RX][RY]=0;
//...
while (movement=1)
   {
        if (GetKeyState(VK_ARROWDOWN))
        { 
          if !( PlayerY=Ymin)
          {
          PlayerPosition [PlayerX][PlayerY]=0
          PlayerPosition [PlayerX][PlayerY-1]=1
          PlayerY=PlayerY-1;
          }
        }
    //This goes on for the other three Arrow Keys
    //...
}

Anybody know how to code this?
PeterK
P.S.: The code is just an example and probably doesn't follow the correct C++ Syntax.
Last edited on
What linux are we talking about here? Anyways, your best bet would probably be to get yourself some specialized library for games, like SFML, SDL or Allegro. Those provide pretty much everything you generally need for a game (graphics/sound, input routines, stuff like that).

Other than that you could also use a more general framework like wxWidgets or Qt.

PS: Arrays are evil, and the same goes for functions like GetKeyState. Input should be handled event based, not poll based.
Last edited on
My whole program is based on an array... Don't want to change it now ;-)
I was going to work with Qt, installed it, but I didn't use it for key input. Could you please post an example code?
Peter
P.S.: thx for helping :)
Also, your main function doesn't have a return type...
argh.

Oh well, an example? I suppose you already know how to create a Qt application? Well then, a full Qt key example:

1
2
3
4
5
6
7
8
9
10
11
12
//main.cpp
#include <QApplication>
#include "Keypress.h"


int main(int argc, char** argv)
{
	QApplication app(argc, argv);
	KeypressWindow w;
	w.show();
	return app.exec();
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//Keypress.h
#ifndef KEYPRESS_WINDOW_H
#define KEYPRESS_WINDOW_H

#include  <QKeyEvent>
#include  "ui_MainWindow.h"

class KeypressWindow : public QMainWindow, public Ui::MainWindow
{
	Q_OBJECT
	
	public:
		KeypressWindow(QMainWindow *parent = 0);
		~KeypressWindow();
		virtual void keyPressEvent(QKeyEvent *event);
	private slots:
		void closeHandler();
};



#endif //KEYPRESS_WINDOW_H 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
//Keypress.cpp
#include "Keypress.h"

KeypressWindow::KeypressWindow(QMainWindow *parent) :
		QMainWindow(parent)
{
	setupUi(this);
	connect(actionClose,SIGNAL(triggered()),this,SLOT(closeHandler()));
}

KeypressWindow::~KeypressWindow()
{
}

void KeypressWindow::closeHandler()
{
	close();
}

void KeypressWindow::keyPressEvent(QKeyEvent *event)
{
	switch(event->key())
	{
	case Qt::Key_A:
		label->setText("A pressed");
		break;
	case Qt::Key_B:
		label->setText("B pressed");
		break;
	default:
		label->setText("Something else pressed");
		break;
	}
}


MainWindow.ui

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>300</width>
    <height>200</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralwidget">
   <widget class="QLabel" name="label">
    <property name="geometry">
     <rect>
      <x>90</x>
      <y>60</y>
      <width>131</width>
      <height>17</height>
     </rect>
    </property>
    <property name="text">
     <string>No Key pressed</string>
    </property>
   </widget>
  </widget>
  <widget class="QMenuBar" name="menubar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>300</width>
     <height>25</height>
    </rect>
   </property>
   <widget class="QMenu" name="menuFile">
    <property name="title">
     <string>File</string>
    </property>
    <addaction name="actionClose"/>
   </widget>
   <addaction name="menuFile"/>
  </widget>
  <widget class="QStatusBar" name="statusbar"/>
  <action name="actionClose">
   <property name="text">
    <string>Close</string>
   </property>
  </action>
 </widget>
 <resources/>
 <connections/>
</ui>

Last edited on
Wow, thanks for all that code, I'm going to try it out tomorrow afternoon, hope I find some time to do that^^
Honestly, I didn't expect THAT much... thanks!
Can I write that .ui file with notepad and just save it that way or do I need some sort of compiler?
Last edited on
You can just save it, but you will need the qt library (and the tools qmake and make) to compile this. If you want to make own GUI's it would probably best to get a tool like Qt Designer.

If you have all of those, put them all into a new directory and just do this from a terminal (with the directory you just created as the active one of course)

qmake -project
qmake
make
Topic archived. No new replies allowed.