generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripters
Home Search
 

Re: Would this be a good way to handle keyboard input?

Previous Thread :: Next Thread 
popinman322 is not online. popinman322
Joined: 04 Mar 2009
Total Posts: 5184
10 Jun 2012 12:54 PM
I have a keyboard class that accepts all keyboard input, and inside of that I have an array of booleans that represents keys down. It also houses a queue of keys typed...

@{Post.TITLE}
Report Abuse
stravant is not online. stravant
Forum Moderator
Joined: 22 Oct 2007
Total Posts: 2893
10 Jun 2012 12:57 PM
Make sure that it only _accepts_ keyboard input, and doesn't listen for it directly. It could be very useful later to be able to tell it that a key was pressed when it actually was not, or filter certain key presses.
Report Abuse
nightname is not online. nightname
Joined: 10 Jun 2008
Total Posts: 8960
10 Jun 2012 12:59 PM
I am pretty sure you are using Java, and in that case, you should use a HashMap. What I do is this (This has been edited to allow me to post it on ROBLOX):

public static final int BUTTON_UP = 0;
public static final int BUTTON_LEFT = 1;
public static final int BUTTON_RIGHT = 2;
public static final int BUTTON_DOWN = 3;

public boolean[] buttons = new boolean[8];
private boolean[] keys = new boolean[256];
private Map[Integer, Integer] buttonMap = new HashMap[Integer, Integer]();

public InputManager() --
bind(KeyEvent.VK_SPACE, BUTTON_UP);
bind(KeyEvent.VK_LEFT, BUTTON_LEFT);
bind(KeyEvent.VK_RIGHT, BUTTON_RIGHT);
bind(KeyEvent.VK_DOWN, BUTTON_DOWN);

bind(KeyEvent.VK_W, BUTTON_UP);
bind(KeyEvent.VK_A, BUTTON_LEFT);
bind(KeyEvent.VK_D, BUTTON_RIGHT);
bind(KeyEvent.VK_S, BUTTON_DOWN);
--

public void bind(int keyCode, int button) --
if ((keyCode >= 0) && (keyCode < keys.length)) --
buttonMap.put(keyCode, button);
--
--
Report Abuse
NXTBoy is not online. NXTBoy
Joined: 25 Aug 2008
Total Posts: 4533
10 Jun 2012 01:01 PM
Roblox doesn't accept curly braces?
Report Abuse
Radioaktiivinen is not online. Radioaktiivinen
Joined: 25 Apr 2009
Total Posts: 18629
10 Jun 2012 01:03 PM
I think it is.

Now go and build a class that takes in a context.

The context defined different key combinations and single keys and actions they trigger (which could be enums?)

liek

"A" > Context.Action(ACTION_KEY_A,1)
"A" + "CTRL" > Context.Action(ACTION_FIRE_DEM_NUKES_K_K,1)
"*move mouse on x axis*" > Context.Action(ACTION_MOUSE_MOVED_X,mouse.x)
"*screen savor activated*" > Context.Action(ACTION_Y_U_AFK,1)


k
Report Abuse
NXTBoy is not online. NXTBoy
Joined: 25 Aug 2008
Total Posts: 4533
10 Jun 2012 01:04 PM
Also, this would be a prime candidate for an enum:

    public static final int BUTTON_UP = 0;
    public static final int BUTTON_LEFT = 1;
    public static final int BUTTON_RIGHT = 2;
    public static final int BUTTON_DOWN = 3;

I think it would be better as:

    public enum ButtonActions { UP, LEFT, RIGHT, DOWN }
Report Abuse
Radioaktiivinen is not online. Radioaktiivinen
Joined: 25 Apr 2009
Total Posts: 18629
10 Jun 2012 01:05 PM
Liek have:
-Classes which take input from controllers
-Something to maintain current context
-For each context, something to translate controller signals to context actions
Report Abuse
popinman322 is not online. popinman322
Joined: 04 Mar 2009
Total Posts: 5184
10 Jun 2012 01:06 PM
@Stravant

The actual setup is:
Keyboard - Class
InputController - Internal Class, implements KeyListener

keyMap - HashMap[Integer, Boolean]
typed - ArrayDeque[Character]
controller - InputController
connectedTo - Component

connectTo(Component) - void
disconnect() - void
setKeyDown(int, boolean) - void
typeKey(char) - void
isKeyDown(int) - boolean
getLastTyped() - char

The InputController is connected to the connectedTo component, then it communicates with the Keyboard instance.
Report Abuse
Radioaktiivinen is not online. Radioaktiivinen
Joined: 25 Apr 2009
Total Posts: 18629
10 Jun 2012 01:08 PM
If you do the mouse like

context.Action(MOUSE_MOVED_X,1)
where 1 is the amount it moved, you could let the user map lets say the arrow keys to it so he doesnt need a mouse. (might need some sensitivity adjustments tho xD) and the user would need to set the key to repeat or something.

or he could map the mouse wheel to the y axis, so that he can move the mouse freely on x axis and it wont move a single pixel on y without him touching the mouse wheel.
Report Abuse
NXTBoy is not online. NXTBoy
Joined: 25 Aug 2008
Total Posts: 4533
10 Jun 2012 01:08 PM
Y U NO PUT ON GIT REPO?
Report Abuse
popinman322 is not online. popinman322
Joined: 04 Mar 2009
Total Posts: 5184
10 Jun 2012 01:10 PM
@Nxt

It's not part of the GreaterGalaxies project, it's called Lightshow. :/

"Mini-Project Lightshow by Popinman322 and Nightname of ROBLOX"
Report Abuse
nightname is not online. nightname
Joined: 10 Jun 2008
Total Posts: 8960
10 Jun 2012 01:10 PM
@NXT

Enums would work in this case, but in my newer input handlers, I designed it in such a way that Enums wouldn't be the best option. Anyhow, this is old code from the past, maybe I should have gave you a newer version.

In fact, you can go further and create your own Key class, and then bind them together. The Key class can tell whether the key is clicked, held down, etc.
Report Abuse
popinman322 is not online. popinman322
Joined: 04 Mar 2009
Total Posts: 5184
10 Jun 2012 01:13 PM
Lol, I just added a single character cache so that getLastTyped() doesn't always return (char)-1 once the last character has been read. :3
Report Abuse
NXTBoy is not online. NXTBoy
Joined: 25 Aug 2008
Total Posts: 4533
10 Jun 2012 01:13 PM
Jumped to conclusions :P

Your quotes tricked me into googling that, only to find that nothing turned up...


@nightname: Why wouldn't enums be better here?
Report Abuse
myrkos is not online. myrkos
Joined: 06 Sep 2010
Total Posts: 8072
10 Jun 2012 01:15 PM
What I have in my latest project is a User class which basically serves as an interface between the window and game logic. The User class has a Direction for the arrow keys, bool for the space key which is the "action" key and a vector for the position of the mouse. The Window class polls for these events and then calls the User class, and the game code then checks the User class for various things.

I think it's a good design, idk
Report Abuse
popinman322 is not online. popinman322
Joined: 04 Mar 2009
Total Posts: 5184
10 Jun 2012 01:17 PM
@myrk

Seems legit.

@Post

Since the cache isn't always a good thing, like when you're editing a text field, I added a boolean to control cache usage...

Ideas? :/
Report Abuse
nightname is not online. nightname
Joined: 10 Jun 2008
Total Posts: 8960
10 Jun 2012 01:18 PM
"@nightname: Why wouldn't enums be better here?"

They would be better here. Did I suggest they won't? If so, sorry, but that is a misunderstanding. What I meant was that in my newer InputHandler class, I wouldn't need to use Enums for that specific thing due to the way I made it.

Report Abuse
NXTBoy is not online. NXTBoy
Joined: 25 Aug 2008
Total Posts: 4533
10 Jun 2012 01:57 PM
> If so, sorry, but that is a misunderstanding.

Nope, my bad. I intended "Why wouldn't enums be better here" to mean "Why wouldn't enums be better for your new method".
Report Abuse
NXTBoy is not online. NXTBoy
Joined: 25 Aug 2008
Total Posts: 4533
10 Jun 2012 01:58 PM
Also, it's not a case of "needing to use Enums", but of "Using enums when what you're doing is better described with enums".
Report Abuse
nightname is not online. nightname
Joined: 10 Jun 2008
Total Posts: 8960
10 Jun 2012 04:01 PM
NXTBoy,

I didn't use Enums for my new method because I wanted to allow the user to change the keys, instead of me having to define every single combination.
Report Abuse
nightname is not online. nightname
Joined: 10 Jun 2008
Total Posts: 8960
10 Jun 2012 04:03 PM
> Continuation post

They keys were different this time. The UP key did something different to the DOWN key, rather than just being a boolean - so some of them had to be special! Using Enums would have meant that they were all the same subset key.
Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripters
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image