Read key input in real-time

I am working on a game in Ogre3D, and have OIS as my input library. I also have 2D graphics to display text glyphs and sprites for the GUI. Right now I am at a point where I need to add text input to the game.

I have searched around for days on how to simply "output what keys I have pressed in a single frame", but all of my results have revolved around the same issue that does not match mine, commonly resulting in "how to CHECK if a certain key was pressed." which is absolutely NOT what I need, in fact it is the opposite. I need a way to (every single frame) output to console (just to make sure it works, then I can store it into a string later) what keys were pressed in a single frame. No conditions: no "is 'A' pressed or is 'B' pressed", just output what was pressed: "you pressed 'A', 'G', and 'Y' in this frame".

I think the best way to know what was pressed is to use printf

printf("You pressed the /s key.\n",KeyPress);

KeyPress is a list of all the keys that were pressed in the specific frame, in real-time.

THAT is all I need. Once I know that no matter what key is pressed, it will be outputted to the console, I can then send those keys to a string, and have a proper text editor class to ensure that specific keys are allowed in the output, and the string will be used by the game to be displayed properly.

NOW if there is a library I can use to make this happen (I do NOT need an entire graphics library for this, I have that portion already. I only need to read in the keypresses unconditionally), then by all means post that. At least I am getting an answer and not "be more specific". How can I possibly make this more specific??
I think the best way to know what was pressed is to use printf

printf("You pressed the /s key.\n",KeyPress);

KeyPress is a list of all the keys that were pressed in the specific frame, in real-time.
printf is supporting a very limited amount of types. So it is not a choice at all.

First of all, you need to know which keys are pressed. Usually (you did not state your platform, so no concrete examples) you just loop over all keys and request if each key is pressed or not, inserting pressed keys in your list. Alternatively you can catch keypress events and push key on each one. When it is time, you output list and clear it, so it is ready to be filled again.

Which 2D libraryare you using? Many of them are multipurpose and have own input handling methods.
Actually, I do not have a 2D library with input support. In ogre3D, it comes with OIS, which handles the keyboard input. OIS also has a derivable class to read what keys are pressed. As for the 2D library, I use Gorilla2D (which is an extension from Ogre3D made by the community) to display sprites and text.

Also my apologies on the platform, I use Windows (the game is targeted to the windows platform as a whole, -maybe- it will be ported to linux). I also use Code::blocks and MinGW as my IDE and compiler, if that helps.

The derivable class is as follows (in OISKeyboard.h):

1
2
3
4
5
6
7
8
9
10
11
/**
		To recieve buffered keyboard input, derive a class from this, and implement the
		methods here. Then set the call back to your Keyboard instance with Keyboard::setEventCallback
	*/
	class _OISExport KeyListener
	{
	public:
		virtual ~KeyListener() {}
		virtual bool keyPressed(const KeyEvent &arg) = 0;
		virtual bool keyReleased(const KeyEvent &arg) = 0;		
	};
If you have input support provided by library it is good, you will not need to use platform-specific API.

so you would need a class derived from KeyListener which receives key presses and stores information about them in some container. Also it needs a method which will clear said container (or some other way to empty container).

So you would request pressed keys container and iterate over it. Then you call clear/whatever to empty input log so class will start to fill it again.
I see, alright. As for attempting a solution on my end, I have found a snippet from the Ogre3D wiki in attempt to replicate it in my program.

The snippet is here. I will edit or repost on what I have found through it.

http://www.ogre3d.org/tikiwiki/tiki-index.php?page=Simple+keyboard+string+editing&structure=Cookbook

With that sample, I have also made another class to derive from KeyListener as a test.

1
2
3
4
5
6
7
8
9
10
struct TestKeyListener: public OIS::KeyListener{
        public:
            EditString myString;
		bool keyPressed(const OIS::KeyEvent &arg){
            myString.injectKeyPress(arg);
             std::cout<<myString.mText<<std::endl;
            return true;
		}
		bool keyReleased(const OIS::KeyEvent &arg){};
    };



EDIT: Alright, I compiled fine with the example and the following Key Listener implementation.

1
2
3
4
5
6
7
8
9
10
struct TestKeyListener: public OIS::KeyListener{
        public:
            EditString myString;
		bool keyPressed(const OIS::KeyEvent &arg){
            myString.injectKeyPress(arg);
            std::cout<<myString.getText()<<std::endl;
            return true;
		}
		bool keyReleased(const OIS::KeyEvent &arg){};
    };


In the initialization of the program, I also set the listener of the keyboard...

myKeyboard->setEventCallback(&myKeyListener);

Now with that added, I am not getting any string output to the console, which is making me wonder if I am missing something else that I do not know about...
Last edited on
Topic archived. No new replies allowed.