Jump to content

Getchar and special keys ?


Wchris
 Share

Recommended Posts

Hello,

 

First, i don't want to use keyhit/keydown because the command requires me to know what key will be pressed. I don't want to specify what key was pressed.

Instead i want to use GETCHAR to get the keycode that was pressed and launch an event. Some kind of callback that would launch if any key was pressed, and then tell me what key it was.

 

but if i say :

key = GetChar();
if (key==KEY_A) { DoSomething(); }

it works

 

and if i say :

key = GetChar();
if (key==KEY_UP) { DoSomething(); }

it doesn't

 

i can't get GETCHAR to work with special keys. A i doing something wrong ?

 

i saw masterxilo using hooks and callbacks in the old forum to achieve this http://forum.leadwerks.com/viewtopic.php?f=2&t=3236&p=28267&hilit=hook#p28267

 

but his solution is not cross platform, so i would prefer a pure LE GETCHAR solution.

 

Any idea ?

 

Thank you

Windows 7 home - 32 bits

Intel Quad Q6600 - nVidia GTX 460 1GB - 2 GB RAM

Link to comment
Share on other sites

I seem to remember that GetChar() only returned printable keys.

 

Why don't you want to have to know what key is pressed? I can understand if you are printing the key to screen or something but KEY_UP can't be printed to screen so you would need to check for it anyway, so then why not use KeyHit()/KeyDown()?

Link to comment
Share on other sites

I seem to remember that GetChar() only returned printable keys.

 

Why don't you want to have to know what key is pressed? I can understand if you are printing the key to screen or something but KEY_UP can't be printed to screen so you would need to check for it anyway, so then why not use KeyHit()/KeyDown()?

 

well is ask here in the c++ forum because the pascal forum is empty so i have more change to get an answer here.

But in fact i'm trying to create a delphi component for LE. Components are 'event driven objects' like in the screenshot

post-44-12678232650751_thumb.jpg

so you see the keydown event launches the Formkeydown method where you get the key.

 

So if i want to write my own component i have to be able to send the key that was pressed to the event method.

 

understood ?

Windows 7 home - 32 bits

Intel Quad Q6600 - nVidia GTX 460 1GB - 2 GB RAM

Link to comment
Share on other sites

I would use a function to pull the key ascii codes not the character.

 

That way you can if then all keys.

 

Sorry I must edit that a little..

 

I just realized the up, down, etc.. will not be in those.

AMD Phenom II x6 1100T - 16GB RAM - ATI 5870 HD - OCZ Vertex 2 60GB SSD

76561197984667096.png

Link to comment
Share on other sites

I would use a function to pull the key ascii codes not the character.

 

That way you can if then all keys.

Hi Victor.

What kind of function ? LE does only provide GetChar to return keycodes.

Windows 7 home - 32 bits

Intel Quad Q6600 - nVidia GTX 460 1GB - 2 GB RAM

Link to comment
Share on other sites

Yeah I understand. I did the same thing that mimics the .NET way. The KeyPress() event is fired for printable keys and the KeyDown/KeyUp events are for all keys and pass to it the VK_ key. Which in this case would be the KEY_ keys.

 

So what I did was call GetChar() and if the value was >= 32 I would fire the KeyPress event passing in the result from GetChar(). Then I created an array of all the KEY_ values (which is pretty easy because most are in sequence). Then looped through this array to see if KeyDown() on any. When it was I sent the value to the event.

Link to comment
Share on other sites

Yeah I understand. I did the same thing that mimics the .NET way. The KeyPress() event is fired for printable keys and the KeyDown/KeyUp events are for all keys and pass to it the VK_ key. Which in this case would be the KEY_ keys.

 

So what I did was call GetChar() and if the value was >= 32 I would fire the KeyPress event passing in the result from GetChar(). Then I created an array of all the KEY_ values (which is pretty easy because most are in sequence). Then looped through this array to see if KeyDown() on any. When it was I sent the value to the event.

 

YES ! Fantastic ! So you encoutered the same problem as me ? GREAT ! (not that you had a problem, but that it was the same as me ;) ) I was so desperate nobody would understand and have the same problem as me.

 

OK this would work ... but between each frame you'll make a useless loop on all special keycodes that will eat FPS where LE could perfectly send you the right keycode with getchar :)

 

i tryed to mail josh and explain this to him, but i think i failed.

 

if there's no better solution i'll do like you ... but it's really frustrating

 

If getchar only return characters because it's called "getCHAR"

 

then we could need a "getKEYCODE" function that would return all pressed keycodes available ... well ... except if LE also needs to achieve a loop on all special keys to achieve this, of course. But normaly on windows you don't have to, windows sends the right keycode to the focused window.

Windows 7 home - 32 bits

Intel Quad Q6600 - nVidia GTX 460 1GB - 2 GB RAM

Link to comment
Share on other sites

Look into kbhit();

 

It's a c++ non blocking method to tell when a key was hit. It should be cross platform also. You can use this to only loop through the array when a key is hit.

 

http://www.cprogramming.com/fod/kbhit.html

 

My bad, this isn't crossplatform. You would have to find the other platform equivalents and use a define when compiling it.

 

I would think BMax would have something like what you are asking for since it's cross platform, but maybe Josh isn't exposing it.

Link to comment
Share on other sites

second link....

 

The library requires programmers to call glutMainLoop(), a function which never returns. This makes it hard for programmers to integrate GLUT into a program or library which wishes to have control of its own event loop.

AMD Phenom II x6 1100T - 16GB RAM - ATI 5870 HD - OCZ Vertex 2 60GB SSD

76561197984667096.png

Link to comment
Share on other sites

I think GetAsyncKeyState is your answer

 

// get async status on all keys
SHORT myKeyboardState[256];
for (int x = 0; x < 256; x++)
 myKeyboardState[x] = GetAsyncKeyState(x);

// now if you want to know about the up arrow key you do this
if(myKeyboardState[VK_UP] & 0x8000) { // check for most significant bit
   // it's currently being pressed
}

Desktop:

Intel 2600K - Gigabyte Z68X-UD3H-B3 - 16GB G.Skill Sniper - EVGA 580 gtx - Raid0 OCZ Vertex 3 SSD - Win7 64bit.

Link to comment
Share on other sites

well, that's even more confusing to me... this is the leadwerks forum right? I only get engine.dll and engine.exe... so, what exactly does he mean by crossplatform?

Desktop:

Intel 2600K - Gigabyte Z68X-UD3H-B3 - 16GB G.Skill Sniper - EVGA 580 gtx - Raid0 OCZ Vertex 3 SSD - Win7 64bit.

Link to comment
Share on other sites

I can only assume he's preparing for when LE becomes crossplatform?

Yes. LE is written in blitzmax, a crossplatform language, and uses OpenGL a cross platform 3D api, so it would be a logical next step. But ok, even if josh said this could happen there is no guarantee yet.

 

There are also some advantages of using a third party input library like joystick support, http://leadwerks.com/werkspace/index.php?/topic/505-joystick-support/page__p__4860__hl__joystick__fromsearch__1entry4860

 

i don't really need joystick yet in my game, but i want to provide the most open solution.

 

Thank you for your kind answers it helped me alot to decide.

Windows 7 home - 32 bits

Intel Quad Q6600 - nVidia GTX 460 1GB - 2 GB RAM

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...