# Problem with callback functions and GLFW

This topic is 5048 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi, I'm using GLFW to write a little game. You can find the homepage of glfw at glfw.sf.net . Now I've come to the point where I need to get key input. With GLFW there are several ways of doing this. You can pass a callback function to GLFW and he calls then the function whenever a key was released or pressed. Now I've setup the ProcessInput() method of my GameEngine class as a static method and was following the prototype proposed in the documentation. But when passing the method to GLFW nothing happens. Everything compiles fine but somehow GLFW doesn't call the function when the user presses a key. I've been at this for a 5 hours now and I've been pulling my hair out about this. I hope somebody can help. Thanks in advance!

##### Share on other sites
Nevermind, found the Edit button.

SORRY for this.

##### Share on other sites
When you edit, there's also a checkbox to delete the post (if, say, you doubleposted).

You might want to show some [relevant] source code. People here can't "magically divine" what your problem is.

##### Share on other sites
// Here's how i define my method inside of my GameEngine class:static void GLFWCALL ProcessInput(int key, int action);// and the implementation:void GameEngine::ProcessInput(int key, int action){	cout << "IT WORKED!!!!!" << endl;}// this is the function i call inside of the GameEngine constructor:	glfwSetKeyCallback(this->ProcessInput);

I think this should be the relevant code. If not I've uploaded all of the source to my webspace.
http://www.malwasandres.de/gameengine.zip

Sorry for not posting this earlier, I thought I've described my problem well enough.

##### Share on other sites
You probably have to call it like this:
glfwSetKeyCallback(&GameEngine::ProcessInput);

##### Share on other sites
Nope, didn't work. I must be doing a stupid mistake somewhere. Or maybe GLFW has a bug somewhere?.

When thinking about this, I more and more come to the conclusion that a callback function is not the best way to do this. I mean the callback function in GLFW just returns keystate changes while the glfwGetKey() function returns the current state of the key. I think it should be enough to update the key states somewhere after a frame was rendered. I mean when a callback function is called it could be in the middle of the drawing of a frame and this could lead to problems.

##### Share on other sites
1) are you calling either glfwSwapBuffer or glfwPollEvents?
2) is a window open?
3) it might be that glfwSetKeyCallback requires a C function?
__a) try compiling glfw as a C++ library
__b) try making it a regular C function
__c) try making it a regular C function and make a pointer to it in your class-really have no idea on that one
__d) on the glfw forum,
https://sourceforge.net/forum/forum.php?forum_id=247562
someone mentioned functors, anyway there are a couple threads on this there

1. 1
2. 2
3. 3
4. 4
Rutin
12
5. 5

• 12
• 18
• 10
• 14
• 10
• ### Forum Statistics

• Total Topics
632662
• Total Posts
3007702
• ### Who's Online (See full list)

There are no registered users currently online

×