Can't define custom actions


#1

I’ve created a custom mapping with json, which seems to be working just fine. Now I’m trying to tie it to a custom action, but it seems impossible to define one. What am I missing?

Controller.Actions.TEST_ACTION_T = function(value){
print(“The custom test action has been triggered by” + value);
};

When I enter Controller.Actions.TEST_ACTION_T into the console, it comes back as undefined. I tried assigning just a string value to it, and it still comes back as undefined. Am I missing something? The rest of my script works fine. Here’s the whole thing:

Controller.Actions.TEST_ACTION_T = function(value){
print(“The custom test action has been triggered by” + value);
};

mappingJSON = {
“name” : “Just testing”,
“channels” : [
{ “from”: “Keyboard.T”, “to”: “Actions.TEST_ACTION_T” }
]
}

mapping = Controller.parseMapping(JSON.stringify(mappingJSON));
mapping.enable();

Controller.keyPressEvent.connect(function(event){
print ("You pressed: " + event.text);
});


#2

I think the Controller API only supports the predefined Actions right now…

There might still be a way to do what you want though – could you explain a bit more about what the T key should do when pressed?

A custom mapping might not be needed, but if you wanted to experiment with responding to Keyboard.T that way you could use the direct API (ie: instead of or in addition to a JSON mapping):

var mapping = Controller.newMapping('Just testing');
mapping
  .from(Controller.Hardware.Keyboard.T)
  .to(function(state, key) {
    print('Keyboard.T is:', state === 1 ? 'down' : 'up');
  });
mapping.enable();
Script.scriptEnding.connect(mapping, 'disable');

#3

Hey humbletim, thanks for your reply!

I have nothing particular in mind for the T key - I’m intrigued by HiFi’s potential as a gaming platform, and being able to define custom actions vs using the direct api would go a long way towards keeping code more maintainable. Do you have any tips for where to start looking in the source code for how to change this? I’m brand new to the code base, but I’d be interested in adding support for definable custom actions.


#4

It’s possible Actions represent a similar but different concept to what I first thought, but here are some best guesses about where to start looking:

It might also be useful to dump exported JS properties out to the console, and then compare with things seen on the C++ side:

Object.keys(Controller).forEach(function(prop) {
  print(prop);
});

Object.keys(Controller.Actions).forEach(function(name) {
  print(name, Controller.Actions[name]);
});

/* ... prints:
ACTION1 269614849
ACTION2 269680385
BOOM_IN 539950849
BOOM_OUT 540016385
Backward 539295489
[...]
*/

#5

Ahhh yes, I see right in the comments - they mention actions currently being hardcoded, with intent to change this in “the near future” hehe. Given that it hasn’t been touched in over a year, I think I’ll dive in! Thanks so much for your help :slight_smile: