• Advertisement
Sign in to follow this  

question regarding keys...

This topic is 3635 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

Ok, so Im trying to figure out key input here, and this is what i have thus far This program opens a console, escape closes it, and its testing the space bar. It should flood the screen with '.'s if the space bar is up, and will flood ' 's if its down. But all it does is flood spaces. Even if I swap defines of K_UP and K_DOWN.
#include <stdio.h>
#include <windows.h>

#define KEY_DOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)
#define KEY_UP(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 0 : 1)

#define K_UP 0
#define K_DOWN 1

void getkeys();

int oldkeys[256];
int newkeys[256];

int main() {

	while(TRUE)
	{
		getkeys();

		if (newkeys[VK_SPACE] = K_UP)
			printf(".");
		else if (newkeys[VK_SPACE] = K_DOWN)
			printf(" ");

		if ( KEY_DOWN(VK_ESCAPE) )
			break;

	}

	return 0;
}

void getkeys()
{
	int key;
	for (key = 0; key < 256; key++)
	{
		if (KEY_DOWN(key))
			newkeys[key] = K_DOWN;
		else
			newkeys[key] = K_UP;

		oldkeys[key] = newkeys[key];
	}
	
}

Share this post


Link to post
Share on other sites
Advertisement
>.<

I decided i was really going to try and do this one all by my self. I figure it out but miss a lil == action... hahaha

thanks!

Share this post


Link to post
Share on other sites
this could be rewritten as follows

// you only need this function
// IsKeyUp is the same as !IsKeyDown(vk)
#define IsKeyDown(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) != 0)

// encapsulates them into one 32 bit integer structure
struct key { short prev, current; } keys[256];
void getKeys(void);

int main() {

while(TRUE) {
getKeys();

if (keys[VK_SPACE].current == 0)
printf(".");
else // no need for else condition
printf(" ");

// matter of opinion (keys) or (keys == 1) - both evaluate the same
if (keys[VK_ESCAPE].current)
break;

}

return 0;
}

void getKeys()
{
int key;
for (key = 0; key < 256; key++)
{
// no point in having this after the update since its never saving the old status! gotta save the current status BEFORE updating
keys[key].prev = keys[key].current;

// IsKeyDown evaluates to 1 or 0 - no need for big if-else block
keys[key].current = IsKeyDown(key);
}
}

Share this post


Link to post
Share on other sites
Quote:
Original post by BreathOfLife
>.<

I decided i was really going to try and do this one all by my self. I figure it out but miss a lil == action... hahaha

thanks!

Turn on all compiler warnings and you'll catch these much quicker.

Share this post


Link to post
Share on other sites
Further improvements, assuming C++:


#include <iostream>
// etc.

// For years now, compilers have had a pretty good idea of how to inline
// functions for performance, so please don't abuse the preprocessor: write
// actual functions where you want functions.

bool IsKeyDown(int vk_code) {
return (GetAsyncKeyState(vk_code) & 0x8000) != 0;
}

// Since we're storing the key state as an encoded value, rather than the
// short returned by GetAsyncKeyState directly, there's no need to use shorts
// here. Also, let's give the struct a reasonable name, and avoid globals.
struct keyState {
bool prev, current;
};

// Define the function up here, and then you don't need a separate prototype.
void getKeys(keyState states[256]) {
// We can declare the counter variable directly in the loop initializer:
for (int key = 0; key < 256; ++key) {
states[key].prev = states[key].current;
states[key].current = IsKeyDown(key);
}
}

int main() {
keyState states[256];
// C++ has a real boolean type; let's use it fully.
// Also, let's use a real condition for exiting the loop instead of breaking
// from an infinite loop, because we can easily do so.
while (!states[VK_ESCAPE].current) {
getKeys(states);
// Use the ternary operator for simple logic like this:
std::cout << (states[VK_SPACE].current ? ' ' : ':');
}
// No need to return 0 explicitly in main().
}


Share this post


Link to post
Share on other sites
Zahlman: Are C++ POD arrays guaranteed zero initialized?

Personally, I'd create a struct:

struct keyStates {
keyState state[256];
};


because structs behave much nicer than raw arrays, type-wise.

Share this post


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

  • Advertisement