Key Mapping

0 favourites
  • 13 posts
From the Asset Store
A template which allows you to customise the controls of your game.
  • Good morning, is there a way to map keys easily. For example-

    If A is pressed, behave as if X were pressed.

    If This area of the screen is touched, behave as if A were pressed.

    Thank you.

  • Not sure what you're trying to do exactly, but it seems a simple OR block would solve your problem:

    If Key A is Pressed

    OR

    If Key X is Pressed > Do whatever you want it to do.

    OR

    If this Are is touched

  • brunopalermo Key mapping is a bit more complex then that. For example if you want to let your players change the default controls etc. If anyone has some good examples I would like to see them as well.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • You are correct, an Or block each time would work.

    However I have created an entire complex game with keyboard controls, and now want to map touch screen and gamepad controls.

    I'm just hoping it will be easier than making dozens of Or blocks...

  • You need to use key codes

    I'm not sure what the key codes for each button is, but you can set the text or textbox to show the pressed button's key code. I have done this before.

    Textbox: Set text to Keyboard.LastKeyCode

    The string expression of key code is Keyboard.StringFromKeyCode(Key code number value)

    As for key mapping, you need to save the key codes into variables (global vars). Set the default values to default control buttons, then change the values of variables if the player changes the controls.

    Oh, I was only talking about keyboard, not sure how key mapping works in touch.

  • I did this in one of my projects. It's more difficult than you'd think.

    My approach was similar to what sgn15 is suggesting. Of course, there are no 'mouse codes', so that consisted of checking each mouse button per tick, logging whether it was depressed or not, then using a function to activate the control mapped to the chosen button.

    If I was doing it again, I think I would migrate some of my global vars (keycodes, control states) into a global dictionary.

  • if you want to detect a double tap or combo (like down-right-left combo), reset your textbox every half second (your press and combo presses are quicker than half second anyway) and delete textbox content every half second.

  • What I did was made each input button add to a variable constantly as it's being pressed and test the variables instead of the control button presses. Like 0 = not pressed, 1 = just pressed, > 1 = holding the button down.

  • I think I'll do as corlenbelspar suggests. It'll still be a lot of backwards engineering but it seems the most suitable for my level of competence.

  • I understand key mapping well Burvey, but he didn't ask about that. lol

    As I said before, I'm not sure what exactly you're trying to achieve and I have no idea how your capx goes, but for what I have read here, it'd appear that corlenbelspar 's approach would work fine for you,

  • The approach I mentioned is actually the way my friend does it and he programs in C++ and used to work for Activision. He was kind enough to explain it to me when I was asking him about control schemes. The best part of it is you can even test for the amount of time a button was pressed but you might want to switch to DT instead of adding 1 to it constantly so you avoid framerate dependence.

  • If anyone wants all CHR$ codes here how you can get it

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)