Jump to content
  • Advertisement
Sign in to follow this  

what language can tell what button you press

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

do ou know which language can tell what button you are pressing right when you press it instead of having to press enter first? how about clicking the mouse? im wondering if there is some kind of "getkey" command that can tell the key you press and store it to a variable...i learned this command in ti-basic...so what is the equivilent in C/C++? thx just wonderin [smile]

Share this post


Link to post
Share on other sites
Advertisement
You can do it on any langage that supports events. So in this case for Windows, c/c++/c#/visual basic. It is the KEY_DOWN event.

theTroll

Share this post


Link to post
Share on other sites
To clarify, pretty much any language can handle this situation. However, the OS provides the service, not the language, so in languages like C++ that do not include that kind of functionality in their standard library, you must use the APIs provided to you by the OS that are compatible with your language.

For C and C++, this means you need to learn about the Win32 API.

Share this post


Link to post
Share on other sites
just to add to jpetrie you can use the WinAPI to make a check. It's simple to do and works for windows only.

this is a snippet of some code I use to scan keys, modify it to work and you have a good key scanner.
#include <windows.h>

This allows you to check for a key state in windows, you will need the keycode. This is available from the MSDN.
#define KeyDown(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)
#define KeyUp(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 0 : 1)

You would use something like this to check if a certain key is pressed. Try creating a new thread and running a simple while loop. This will allow you to create custom functions for each key. But, I'm sure it can be modified to check for all keys, never tried. check on GetKeyState() and GetAsyncKeyState.
while(true){
if(KeyDown(VK_ESCAPE)){
// Your key code here
}
}

Share this post


Link to post
Share on other sites
When I was first trying to get immediate key-input, I turned to the conio.h header file that came with Dev-C++. If you are using Dev-C++ (I don't know if this file comes with Visual Studio. I'm sure Visual-Studio has something similar), inlucde the file <conio.h>

Now, to get keyboard input, use getche() to get the key that was pressed and display that character to the screen, or use getch() to get the keypress and not display anything.

So:

#include <conio.h>
#include <iostream>

using namespace std;

int main()
{
char input_char;
input_char = getch();
cout<<input_char<<endl;
return 0;
}


That will display the first key pressed, without waiting for "Enter" to be pressed.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!