GameMonkey Script

GameMonkey Script Forums
It is currently Wed Aug 15, 2018 1:38 am

All times are UTC




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: Keyboard input
PostPosted: Thu Aug 18, 2011 12:18 pm 
Offline

Joined: Fri Jun 19, 2009 12:39 pm
Posts: 31
I'm starting to write some code for dealing with input.

Writing stuff for mouse and touch input should be easy enough....I'll just a build a list of events on the native code side of things with each one having a time stamp. Then my interface code will allow the script to query the list of events. No problem there.

But what about the keyboard? This is something I'm not so sure about. I really wish I could use some sort of macro to define key constants! But since I can't, I'm thinking of doing something similar to the mouse/touch input with the keyboard; each event being a key up or key down with the ID of the key being encoded as an int/id.

So for example my script code for dealing with the space bar might look like;

Code:
    currKeyEvent = KeyEvent();  // currKeyEevent is just a wrapper for dealing with a native key event.
    while(inputManager.getTouchEvent(currKeyEvent))
    {
      if (currKeyEvent.getKey() == 'SPC')
      {
             .....
      }
    }


Or alternatley, I could just create a global table of constants and mimic the windows VK_????? macros. You see, the real issue for me is; how do I have a consistant way of identifying keys? Not so much of an issue on devices without keyboards, but I'd like something simple under my windows build which I'll be using for heavy testing/development.

Any thoughts?


Top
 Profile  
Reply with quote  
 Post subject: Re: Keyboard input
PostPosted: Fri Aug 19, 2011 8:02 am 
Offline

Joined: Mon Dec 15, 2003 1:38 pm
Posts: 703
Just a few thoughts on keyboard input and input in general...

I abstract all mouse, keyboard, joystick, gamepads as devices with a set of buttons and axes. This works well for most input types, with the exception of touch screens.

As a side note, I also generate pseudo buttons from axes so that, for example, holding down axis PAD_AXIS_LEFT_Y generates button PAD_BUTTON_AXIS_LEFT_Y_NEG and thus allows menu traversal via a thumb stick as if a down arrow button was pressed. I also scan the entire state of each connected device in a single phase and allow queries as to the Down, Up, Pressed, Released status implied by the delta of the current and previous states. Keyboards may require extra buffering to catch high frequency key presses at low frame rates.

Anyway, I use native enums to generically identify generic or platform specific devices. So all keyboards have a enum like KEY_A, KEY_SPACE. I use GM script to bind input devices to game/app input handling. eg:

BindInputAxis( ACTION_Turn, DEVICE_Joystick, Joy_Axis_X, 0.265, 0.000, false, true );
BindInputButton( ACTION_Left, DEVICE_Keyboard, KEY_A );
BindInputButton( ACTION_Left, DEVICE_X360Pad, Pad_Button_Axis_Right_X_Neg );

Exposing these Id's to GM is a matter of setting global variables, or if you don't want to pollute the global table, stick them in another table.

Each platform has input handling code which converts IDs for generic devices like mice, keyboards, joysticks into native API IDs, so a Win32 implementation would have an array converting the KEY_A into VK_A. A DirectX version might convert to DIK_A or whatever. The important thing is the game/app can query at any time, the status of input like:
Input()->GetButtonPressed(DEVICE_MOUSE, MOUSE_BUTTON_1)
Input()->GetAxis(DEVICE_GAMEPAD, LEFT_STICK_X)
Higher level code can do fancy action mapping or event handling to modularize input handling or use event logic instead of scattered polling.
You can see I prefer to use script to configure things and run shot bursts of decision logic, rather than implement large parts of application code or do anything intense.


Top
 Profile  
Reply with quote  
 Post subject: Re: Keyboard input
PostPosted: Sat Nov 26, 2011 1:41 pm 
Offline

Joined: Sat Apr 25, 2009 1:40 am
Posts: 66
I'm currently writing a game development engine that uses game monkey. My input lib api looks like this:

Code:
      for(event = Event.Poll(); event; event = Event.Poll())
      {
         switch(event.Type)
         {
            case Event.KEY_PRESS: {
               if(event.KeyCode == Input.VK_UP) {
                  player.Jump(level);
               } else if(event.KeyCode == Input.VK_DOWN)
               {
                  player.Bounce(level);
               }
            }
            
            case Event.QUIT: {
               System.Exit();
            }
         
            case Event.MOUSE_PRESS: {
               if(event.MouseButton == Input.MB_RIGHT) {
                  level.AddObject(BlockDripper(Math.Floor(event.X / 20) * 20, Math.Floor(event.Y / 20) * 20));
               } else if(event.MouseButton == Input.MB_LEFT) {
                  level.AddObject(Block(Math.Floor(event.X / 20) * 20, Math.Floor(event.Y / 20) * 20));
               }
            }
            
            case Event.MOUSE_RELEASE: {
            }
            
            case Event.MOUSE_MOVE: {
            }
            
            case Event.WINDOW_RESIZE: {
               Graphics.ResizeScreen(event.Width, event.Height);
               Graphics.SetViewPort(0, 0, event.Width, event.Height);
            }
         }
      }



And for checking if a key is being pressed:

Code:
      if(Input.KeyCheck(Input.VK_RIGHT))
      {
         player.Move(5 * delta, 0, level.objects);
      } else if(Input.KeyCheck(Input.VK_LEFT))
      {
         player.Move(-5 * delta, 0, level.objects);
      }


So, yeah i define the VK_* in a global table called "Input"

I've attached my EventLib and InputLib which uses SDL.


Attachments:
eventinput.zip [2.67 KiB]
Downloaded 185 times
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB® Forum Software © phpBB Group