PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

Button Handling

Thanks for the great libary, I'm a bit of a noob when it comes to c++ programming but it's really nice and easy to use. So far I've just been making simple programs like bouncing balls and particle sprayers and making them respond to stylus input. Not bad considering I've only ever made about 5 small programs. Anyway....

How do you get button presses from pocket frog? I'm not sure how int button translates to certain buttons. Can anyone provide the code they use in v0.5.0

Thanks
- J
I will assume that you are using the framework (class Game):

There is two methods named "ButtonDown()" and "ButtonUp()". These are called when a key is pressed (ButtonDown) or released (ButtonUp).

The parameter specifies the virtual key code of the button being pressed or released. To know what button actually match the virtual key code, you need to use the key list returned by InputDevice::GetKeyList(). The framework already has an InputDevice object named m_input. So you would do something like this:

Code:

int MyGame::ButtonDown( int button )
{
const KeyList& keys = m_input.GetKeyList();

if (button == keys.vkLeft)
{
// go left...
}
else if (button == keys.vkRight)
{
// go right
}
else if (button == keys.vkA)
{
// Jump...
}


The available keys on a PocketPC are named following the GAPI convention: up, down, left, right, A, B, C and Start. On some devices, extra keys are available (PocketFrog name them Aux1 and Aux2).

Let me know if this helps you!
tdz · 13 Oct 2002, 02:37 UTC — problems with buttons
Im having trouble, but im a newb also.

the button callback seems to work fine from within GameLoop. I can control my game fine.

now that im almost done, ive added a menu system to set some basic options. I do a bool check and if necesary, call Menu() from within GameLoop at the very beginning, so it displays right after the splash.

From within it, im trying to allow the user to hit left and right and choose options, but I always just have to reset my journada. they dont seem to take.

Am I allowed to do this? Or do i have to put all my menu code directly into Gameloop?

TIA,
tdz (will post my stupid game when it works:)
tdz · 13 Oct 2002, 22:46 UTC — oops
im gimpy, it was my fault. buttons workin fine.
I have pasted the code above into the plasma demo and i get errors,how do i set this up for the plasma demo,do i need to declare overrides in the header or any member variables.

also is int the correct return value ,when i look i see void
Conan · 15 Oct 2002, 10:27 UTC — using the button functions in Plasma
Firstly you have not mentioned any specific errors. It's really useful to know what the error is to allow a sensible reply.

The built-in functions for reading button down & up plus the stylus down up & move will always work.

I had a look at the Plasma demo. The game class is called Plasma in this demo not MyGame so any reference to MyGame needs to be changed to Plasma

What happens in 'PF' is that an individual 'instance' of the game class is created with it's own name. Usually everyone just leaves this as 'MyGame' but it can be anything you like.
Ok.... This is driving me nuts!

I just upgraded my app from PF 0.4.5 to PFP 0.2.1, and I got all of my Blitting/Rasterizers fixed and working. But Input is giving me a HUGE problem.

I have my class defined as:
Code:
class DrPocket : public Game
{
private:
int score;
int gameSpeed;
int screen;
bool drawOnce;

public:
bool bPressed[8];
MenuScreen *pMenuScreen;
SelectScreen *pSelectScreen;
GameScreen *pGameScreen;
ScoreScreen *pScoreScreen;
HelpScreen *pHelpScreen;
ConfigScreen *pConfigScreen;
GameOptions *pGameOptions;

DrPocket::DrPocket();
DrPocket::~DrPocket();
void DrPocket::Update();
void DrPocket::ChangeScreen(int);
void DrPocket::UpdateScreen();

public:
virtual bool DrPocket::GameInit();
virtual void DrPocket::GameLoop();
virtual void DrPocket::GameEnd();
virtual void DrPocket::ButtonDown(int);
virtual void DrPocket::ButtonUp(int);
virtual void DrPocket::StylusDown(int, int);
virtual void DrPocket::StylusMove(int, int);
virtual void DrPocket::StylusUp(int, int);
virtual void DrPocket::SplashScreen();
};


So it's supposed to inherit all of the PocketFrog stuff that's in Game, right? However, I keep running into the problem with m_input when I try:


Code:
void DrPocket::ButtonDown(int button)
{
const InputDevice::KeyList& keys = m_input->GetKeyList();

// Button Array
if(button == keys.vkUp) bPressed[0] = true;
...


The compiler tells me that I can't access m_input because it's a private member of Frog::Game.

I tried the code shown earlier in this post, but it says there's a syntax error: ';' required before '&'. So another problem is that it doesn't understand KeyList unless I write it as InputDevice::KeyList... And then when I use the . instead of ->, I see a totally different list of options.

It was so simple back in PF 0.4.5 when you could write:

Code:
switch(button)
{
case BUTTON_UP:
...
break;

case BUTTON_DOWN:
...
break;

...
}


I know there's a very simple solution to this, but it's eluding me. I've just spent the last 4-5 hours trying to resolve this to no avail. If one of you could show me a code snippet, I'd really appreciate it.

If you need more info, don't hesitate to ask. I must say though that not one of the examples packaged with PF 0.6.0 or PFP uses the buttons, and it's really very frustrating to figure out.

Thanks in advance,
-John
Well, I don't know anything about GetKeyList() and using keys and stuff, but I can easily solve the following dilemma for you:

The compiler tells me that I can't access m_input because it's a private member of Frog::Game.


Ofcourse! You can't access a private member by extending a class. Private members are always private, and only visible to the class that defined them as private. A good programming strategy, is to always hide the members (and local object helper functions) as private, and then make them accessible by public (or protected) functions. So, looking at the game.h file I found this out:

Just as you write:
Code:
DisplayDevice* display = GetDisplay();

you should write:
Code:
InputDevice* input = GetInput();


Then, because input is a pointer, you should use the -> syntax to access it's members and functions, so for instance:
Code:
const InputDevice::KeyList& keys = input->GetKeyList();


Then, looking at the input.h file I found out the following:

The GetKeyList() function returns a constant reference to a KeyList struct. And the struct is defined in the InputDevice class, so that's why you need to classify it as 'const InputDevice::'.

And, since keys is a reference to a KeyList object (or struct if you will) you should use the . to access it's members. (. is for objects and references to object, and -> is for pointers to objects only)

It was so simple back in PF 0.4.5 when you could write: switch(button){ case BUTTON_UP: ... }


You can still use the switch-case if you want. What's stopping you??
Code:
switch(button)
{
case keys.vkUp:
...
break;
case keys.vkDown;
...
break;
...
default:
...
}


Hope this'll help you out.

Ps. I have not used namespace qualifiers here since they are unnesessary because it looks like you have defined your DrPocket class whithin the Frog namespace. If you had not (and perhapps you should consider this since the DrPocket is not really part of PocketFrog anymore is it) you must use the Frog namespace qualifier before all the objects OR if you like to make it more simple (but less strict) put a 'using namespace Frog;' on the first row in every function that uses PF objects.
This has nothing to do with anything on this thread, but I can't really post a new note about this either. So, here goes:

I've noticed a lot of you guys learn by studying example code. And while this is good in some situations, it's never a bad idea to have some solid knowledge about WHAT is beeing done and not just how it is done.

For example, the post I just wrote (above) required that I knew something about namespaces, the difference between references and pointers, public vs. private, and then finally looking at the headerfiles (or class definitions if you may) of the relevant objects.

This note should not be interpreted as if I'm angry and condesending towards people who learn by example. I'm just saying that a little background knowledge doesn't hurt.

Always happy to help and share my knowledge in computer languages and computer science.
Thanks adde,

I ended up doing:
Code:
const InputDevice::KeyList& keys = GetInput()->GetKeyList();

My real killer was that I thought an inherited class had access to all of its ancestor's member variables. Now I know better. =)

The reason I had to switch from using the "switch/case" code to "if/else" code is because the compiler reports the following error message that keys.vkUp, keys.vkDown, etc. aren't constant values, so they can't be used in case statements. Using the keyword "const" before "InputDevice::KeyList ..." doesn't help.

error C2051: case expression not constant

Thanks again!
-John
I'm just lazy and use the hex values for the keys... that works too, although it probably really is ugly. By the way, do the button definitions of A, B, C, and Start change from device to device?
Presto wrote:
The reason I had to switch from using the "switch/case" code to "if/else" code is because the compiler reports the following error message that keys.vkUp, keys.vkDown, etc. aren't constant values.


Heh, how ironic! It seems like I too lack som basic fundamental knowledge =) Sorry bout that.

Damian wrote:By the way, do the button definitions of A, B, C, and Start change from device to device?


Well, since the KeyList structure is not predefined you should assume it could change depending on the device (otherwise, there would be no reason why the KeyList shouldn't be predefined so you could use it in a switch-case statement for instance =) But don't take my word for it.
I like to have my code working both on the desktop and the device, and the two weren't consistent. I did try the key values, but only the D-Pad and arrow-keys matched up. When I tried the VK_UP, VK_DOWN, '1', '2', '\r', etc. it worked on the desktop but not the device.

I almost coded:
Code:
#if defined(_WIN32_WCE)
#define BUTTON_UP 0x0026
#define BUTTON_DOWN 0x0028
#define BUTTON_LEFT 0x0025
#define BUTTON_RIGHT 0x0027
#define BUTTON_A 0x00C3
#define BUTTON_B 0x00C4
#define BUTTON_C 0x00C5
#define BUTTON_START 0x0086
#else
#define BUTTON_UP VK_UP
#define BUTTON_DOWN VK_DOWN
#define BUTTON_LEFT VK_LEFT
#define BUTTON_RIGHT VK_RIGHT
#define BUTTON_A '1'
#define BUTTON_B '2'
#define BUTTON_C '3'
#define BUTTON_START '\r'
#endif


But I don't trust that to cover everything. PF has all of the information already inside it, which led to my quest to get it working correctly. =)

-John
It's funny, I too lacked a little bit of basic knowledge that adde just taught me...

I don't think I actually knew what the difference between dot notation and -> was... I think I've always just tried one and if it gave me an error I used the other and never bothered to question why that was...

Just goes to show, no matter how much experience or knowledge you have, you can always learn something, even if it's a basic piece of knowledge that fell through the cracks of your learning early on.
Damian wrote:
I'm just lazy and use the hex values for the keys... that works too, although it probably really is ugly. By the way, do the button definitions of A, B, C, and Start change from device to device?


No, they do change from device to device. That's the reason there is a structure filled in at runtime.
OK... that's what I thought.
I have problems with arrow keys running on the device Ipaq.

I traced the button in ButtonDown( int button )
It always seem to return 132 when i press any of the four arrow keys.

Any ideas?
thanks

Marvin