Forum Topic: detecting key being pressed C++

(356 views • 13 replies)

This topic is 1 page long.

<< < > >>
None

SkratorvanKernel

Reply To Post Reply & Quote

Posted at: 10/5/09 06:50 PM

SkratorvanKernel LIGHT LEVEL 04

Sign-Up: 09/03/09

Posts: 38

hi guys, im making a game in c++, but i can't find any function that detects if a key is BEING pressed... I don't want a function like kbhit(), that detects if key was just pressed... i think you are not getting me :P

ok, i will explain better... i want a function that detects if a key is being pressed, meaning that the function will still telling the game that key is being pressed while the player presses it... and kbhit doesnt do that, it just gets the key "one time".. :/


None

Jon-86

Reply To Post Reply & Quote

Posted at: 10/5/09 06:59 PM

Jon-86 NEUTRAL LEVEL 13

Sign-Up: 01/30/07

Posts: 3,945

The following links will provide you with information and code explaining how you can use hooks under Windows :
- MSDN Library Online : Hooks
( http://msdn.microsoft.com/library/defaul t.asp?url=/library/en-us/winui/winui/win dowsuserinterface/windowing/hooks.asp)

- MSDN Library : Win32 Hooks
by Kyle Marsh
( http://msdn.microsoft.com/library/defaul t.asp?url=/library/en-us/dnwu/html/msdn_
hooks32.asp
)

- MSDN Code Samples : DisableLowLevelKeys.cpp
( http://msdn.microsoft.com/code/default.a sp?url=/msdn-files/026/001/226/Source%20 Files/DisableLowLevelKeys_cpp.asp)
C++ code to trap and disable certain keys like ALT+TAB,ALT+ESC & CTRL+ESC on Windows NT 4 and above.

- CodeProject.com : Articles on DLLs
(http://www.codeproject.com/dll/index.as p#Hooks)
C++ code and tutorials demonstrating the proper use of DLLs for using the various hooks available.

- Keyboard Hook by Gil Dabah & Oren Becker
( http://qsoft.ragestorm.net/tutors/window s/kbhook.html )
Tutorial with sample source to demonstrate use of keyboard hooks using the Win32 API.

- MSDN Magazine > October 2002 : Windows Hooks in the .NET Framework
( http://msdn.microsoft.com/msdnmag/issues /02/10/CuttingEdge/default.aspx)
Article demonstrate how to use hooks from the .NET framework.

PHP Main :: C++ Main :: Java Main :: irc.freenode.net

BBS Signature

None

Jon-86

Reply To Post Reply & Quote

Posted at: 10/5/09 07:01 PM

Jon-86 NEUTRAL LEVEL 13

Sign-Up: 01/30/07

Posts: 3,945

Damn I copy & pasted that from an old file I had, some of the links (which were good) have expired. But for the ones that still work, they should be enough.

Also read into how events and event listeners work.

PHP Main :: C++ Main :: Java Main :: irc.freenode.net

BBS Signature

None

gapern

Reply To Post Reply & Quote

Posted at: 10/5/09 08:17 PM

gapern NEUTRAL LEVEL 08

Sign-Up: 11/03/06

Posts: 91

kbhit looks like a console function so I'm kinda doubting he is using win32, or any library at all. I'm assuming you are making a "rogue" game because I don't see how checking if a key is being held would be valuable with any other type of console game. More information about what you're making and any libraries you are using would be helpful.


None

urbn

Reply To Post Reply & Quote

Posted at: 10/5/09 10:26 PM

urbn FAB LEVEL 18

Sign-Up: 06/10/07

Posts: 2,302

At 10/5/09 08:17 PM, gapern wrote: I don't see how checking if a key is being held would be valuable

Auto fire, walking, running (with shift or something) powering up a charge (like worms) etc.

Plenty of reasons why you may want to check if a key was being held.

BBS Signature

None

gapern

Reply To Post Reply & Quote

Posted at: 10/5/09 11:27 PM

gapern NEUTRAL LEVEL 08

Sign-Up: 11/03/06

Posts: 91

Well yea, I'm aware of the uses of holding a key down, just not in a console game. I'm sure all the actions you mentioned have been done in a console game but judging by the lack of info in the first post I'm going to assume this guy isn't at the skill level to do much of that. That sentence I wrote probably shouldn't have even been mentioned as it wasn't relevant to the topic. Let's not hijack his topic please.


None

Scyllinice

Reply To Post Reply & Quote

Posted at: 10/6/09 10:59 AM

Scyllinice NEUTRAL LEVEL 01

Sign-Up: 03/04/09

Posts: 14

If you are using Windows, you probably want GetAsyncKeyState.


None

BasV

Reply To Post Reply & Quote

Posted at: 10/6/09 12:20 PM

BasV LIGHT LEVEL 20

Sign-Up: 05/07/05

Posts: 303

I think keyboards don't register any keys being up or down - just the event of it going down or back up. If you know these two, you'll always know whether a key is up or down.


None

urbn

Reply To Post Reply & Quote

Posted at: 10/6/09 12:39 PM

urbn FAB LEVEL 18

Sign-Up: 06/10/07

Posts: 2,302

At 10/5/09 11:27 PM, gapern wrote: a console game

Maybe I missed something, but I wasn't aware he specified that it was a console game?

BBS Signature

None

kiwi-kiwi

Reply To Post Reply & Quote

Posted at: 10/6/09 01:34 PM

kiwi-kiwi LIGHT LEVEL 08

Sign-Up: 03/06/09

Posts: 658

If you're using Win API
You can use the way events are handled in your advantage, you make a if (or switch) statement in the callback procedure to handle WM_KEYDOWN and you do this (assuming keyPress is a global variable that was already declared, I suggest using char keyPress[256])

keyPress[wParam]=1;

later in the code

if (keyPress[VK_SHIFT]) //use whatever key you need instead of VK_SHIFT
{
   //do the stuff you want to do 
   keyPress=0;
}

So C++ first gets a message that a key is being pressed and for every message it gets it sets the variable in the array to 1
Afterwards, you check the value in the array to see if the key was pressed. If it was pressed you do what you need to do and then assume it isn't being pressed anymore and set it to 0.
If the guy keeps the key pressed, it will be set to 1 again therefore executing that statement again.

Hope this helped

None

gapern

Reply To Post Reply & Quote

Posted at: 10/6/09 05:09 PM

gapern NEUTRAL LEVEL 08

Sign-Up: 11/03/06

Posts: 91

At 10/6/09 12:39 PM, urbn wrote:
At 10/5/09 11:27 PM, gapern wrote: a console game
Maybe I missed something, but I wasn't aware he specified that it was a console game?

Well looks like the OP took off so I don't feel as bad about possibly derailing the thread.

He didn't specify anything at all. I'm under the assumption it is a console program because when I googled the kbhit function it looked like it was only usable with the console. It also requires conio.h to use, which I think is safe to assume stands for console input output. I'm aware that he is not wanting to use the kbhit function, but if he's referencing a console function then I can assume it's a console game he's trying to make. I could of course be dead wrong here. I'm not a mind reader or an expert at c++.


None

RageOfOrder

Reply To Post Reply & Quote

Posted at: 10/8/09 11:49 AM

RageOfOrder EVIL LEVEL 09

Sign-Up: 08/30/02

Posts: 6,349

A lot of people ask how to do a "press any key to continue" in C that doesn't involve the enter key.

This might be helpful for you somehow.

If you are on a POSIX system (Linux, BSD, etc), this removes the buffer from from getchar() entirely

#include <termios.h>
#include <stdio.h>
#include <unistd.h>

int getch( )
{
        struct termios oldt,
        newt;
        int ch;
        tcgetattr( STDIN_FILENO, &oldt );
        newt = oldt;
        newt.c_lflag &= ~( ICANON | ECHO );
        tcsetattr( STDIN_FILENO, TCSANOW, &newt );
        ch = getchar();
        tcsetattr( STDIN_FILENO, TCSANOW, &oldt );
        return ch;
}

int main()
{
        printf( "Press any key to continue...\n" );
        while( !getch() );
        return 0;
}

At 9/28/09 06:57 PM, citricsquid wrote:
linux isn't for those who want windows. use windows if you want windows.

BBS Signature

None

finaiized

Reply To Post Reply & Quote

Posted at: 10/12/09 01:13 PM

finaiized LIGHT LEVEL 04

Sign-Up: 07/31/09

Posts: 35

I don't use C++ and instead C#, but it's basic logic here if you can only get one key down and key up.

First, you check for one key down. It should fire only once, right? Good. Make a boolean like isDown=true. Now check for a key up. When the key is up, then isDown=false.

So basically, here's some psuedo-code:

bool isDown=false;

if (key is down) {
isDown=true;
}
if (key is up) {
isDown=false;
}

It makes sense, that if you never release the key, then isDown will always be true, thus creating a sense that it will fire more than once. Just create an if statement like if(isDown==true) and put the event like firing missiles in there.


None

robin1232

Reply To Post Reply & Quote

Posted at: 10/12/09 02:54 PM

robin1232 DARK LEVEL 16

Sign-Up: 01/19/08

Posts: 2,618

At 10/12/09 01:13 PM, finaiized wrote: I don't use C++ and instead C#, but it's basic logic here if you can only get one key down and key up.

First, you check for one key down. It should fire only once, right? Good. Make a boolean like isDown=true. Now check for a key up. When the key is up, then isDown=false.

So basically, here's some psuedo-code:

bool isDown=false;

if (key is down) {
isDown=true;
}
if (key is up) {
isDown=false;
}

It makes sense, that if you never release the key, then isDown will always be true, thus creating a sense that it will fire more than once. Just create an if statement like if(isDown==true) and put the event like firing missiles in there.

hell no, C++ is a way different language, XNA (you use that right?) is more like game maker compared to C++, maybe the grammar is nearly the same, but C++ is really different

on topic:
has anyone got an idea how to do this for mac osx?


All times are Eastern Standard Time (GMT -5) | Current Time: 03:35 PM

<< Back

This topic is 1 page long.

<< < > >>
You need a Grounds Gold Account to post on the NG BBS! If you don't have one, click here to sign up now! It's fast, free, and easy — and opens up tons of great NG features!