Custom Controls Extension

Bombini

  • *
  • Posts: 1160
Very cool thanks!
For all who want to see how it looks:

Bombini

  • *
  • Posts: 1160
One last question in the puzzle:

How do i draw the Name of the currently associated Key of a control (without pressing a key)?
I know how to display the name when a key was pressed/changed but i need to get also the name when creating a scene or in the always block. Something like (get name of control).
Example:
  • I define default controls (works)
  • Player changes controls using the method from former posts and the key name change is displayed (works)
  • Game is reloaded and the changed key name  displays  without pressing a key (how=?)
I tried this but this is just giving out the general name of the key. Not the associated control:


captaincomic

  • *
  • Posts: 6109
You can use
Code: [Select]
Input.getControlMap().get("control-name")
to get the list of key codes of the keys assigned to the control.

Or
Code: [Select]
Input.getControlMap().get("control-name")[0]
to just get the key code of the first key assigned to it.

I'm sorry, but I cannot think of an easy way to get the name of the key from the key code, off the top of my head, besides writing a map off all key codes and key names ...

Bombini

  • *
  • Posts: 1160
Thanks a lot!
This is how i did it saving the words into a list):



methodandred

  • Posts: 3
Does this still work in 3.4?

Bombini

  • *
  • Posts: 1160

Bombini

  • *
  • Posts: 1160
The extension does not work in the newest Stencyl version anymore (b10100).
Any idea how to fix it?
Cheers!

Justin

  • *
  • Posts: 4100

For Live Support: Join our discord channel and ping me @justin.

Bombini

  • *
  • Posts: 1160
Hi Justin,

i tested the new version a lot and cant get it to work.
Setting a new control.

It worked fine before.
Did you test it? I am a bit helpless.
Cheers!

Justin

  • *
  • Posts: 4100
Sorry, I realized that not everything was being updated completely with my last edits. I've modified it again (as of a couple days ago) and both the link on the first page and my link up above should give you the latest.

For Live Support: Join our discord channel and ping me @justin.

Bombini

  • *
  • Posts: 1160
Hi Justin,

thanks for the update. I am getting compilation errors now.
I dont understand what to do. I Havent changed the blocks too much from earlier this topic. Logs attached.
Do you knwo whats going on?

Cheers!

Quote
import com.stencyl.Input;
import openfl.ui.Keyboard;

class CustomControls
{
   public static function setControl(control:String, keyCode:Int)
   {
      trace("Set control " + control + " to key code " + keyCode + ".");
      Input.define(control, [keyCode]);
   }
   
   public static function setControlByName(control:String, key:String)
   {
      trace("Set control " + control + " to key " + key + ".");
      if (Reflect.hasField(Keyboard, key))
      {
         var keyCode:Int = Reflect.field(Keyboard, key);
         Input.define(control, [keyCode]);
      }
      else
      {
         trace("Failed to set control " + control + " to unknown key " + key + ".");
      }
   }
   
   public static function addKeyToControl(control:String, keyCode:Int)
   {
      trace("Add key code " + keyCode + " to control " + control + ".");
      Input.mapKey(keyCode, control);
   }
   
   public static function addKeyToControlByName(control:String, key:String)
   {
      trace("Add key " + key + " to control " + control + ".");
      if (Reflect.hasField(Keyboard, key))
      {
         var keyCode:Int = Reflect.field(Keyboard, key);
         Input.mapKey(keyCode, control);
      }
      else
      {
         trace("Failed to add unknown key " + key + " to control " + control + ".");
      }
   }
   
   @:access(com.stencyl.Input._controlMap)
   public static function getControlConfig():Array<String>
   {
      var controlConfig:Array<String> = new Array<String>();
      var controlMap:Map<String,Control> = Input._controlMap;
      for (control in controlMap.keys())
      {
         controlConfig.push(control + ";" + controlMap.get(control).keys.join(","));
         trace(control + ";" + controlMap.get(control).keys.join(","));
      }
      return controlConfig;
   }
   
   public static function applyControlConfig(config:Array<Dynamic>)
   {
      for(i in 0...config.length)
      {
         var controlKeys:Array<String> = cast(config, String).split(";");
         var control:String = controlKeys[0];
         Input.unmapControl(control);
         
         var keyStrings:Array<String> = controlKeys[1].split(",");
         for (j in 0...keyStrings.length)
         {
            Input.mapKey(Std.parseInt(keyStrings[j]), control);
         }
         
         trace("Set control " + control + " to keys " + keyStrings + ".");
      }
   }
}

Justin

  • *
  • Posts: 4100
Ah, sorry, you need to update to at least b10141 as well.

For Live Support: Join our discord channel and ping me @justin.

Bombini

  • *
  • Posts: 1160
Tested and works!
Thanks a lot Justin!