Virtual keyboard/gamepad for external use?

Get help using Construct 2

Post » Thu Jun 22, 2017 11:52 am

Is there a way to trigger a keystroke outside of the construct/browser window when an item is clicked or hovered over?

Example: I click a sprite on the screen and it produces a keystroke outside of the browser as if I physically hit the key myself.

Thank you.
B
12
S
4
G
2
Posts: 132
Reputation: 1,749

Post » Thu Jun 22, 2017 9:41 pm

No, the plugins for keyboard and gamepad does not have any action, but i believe that if you edit the plugins yourself you could add the action to simulate a keystroke
B
5
S
2
Posts: 8
Reputation: 443

Post » Fri Jun 23, 2017 12:44 am

Ok, i have edited the keyboard plugin and here comes a little tutorial:

First go to the directory: YOUR_CONSTRUCT_DIR\exporters\html5\plugins\keyboard

then open the files: edittime.js and runtime.js

basically the first file is where you configure the plugins conditions, actions, etc, the second is where you implement this actions and conditions(some plugins may also need thirdy party plugins, when exporting for cordova you need to install them yourself)

Configuring the actions
Well, there is two actions that you need: keydown and keyup, in the first file choose some place and post this code there:

Code: Select all
////////////////////////////////////////////////////////////
// Actions
// AddAction(id,            // any positive integer to uniquely identify this action
//          flags,            // (see docs) af_none, af_deprecated
//          list_name,         // appears in event wizard list
//          category,         // category in event wizard list
//          display_str,      // as appears in event sheet - use {0}, {1} for parameters and also <b></b>, <i></i>
//          description,      // appears in event wizard dialog when selected
//          script_name);      // corresponding runtime function name

AddKeybParam("Key", "Choose a key.  Note that international users and users on different operating systems or devices may not have the same keys available.");
AddAction(
   0,
   af_none,
   "Simulate keydown",
   "Keyboard Simulation",
   "Simulate keydown {0}",
   "Simulate a keydown from the keyboard",
   "send_keydown"
);

AddKeybParam("Key", "Choose a key.  Note that international users and users on different operating systems or devices may not have the same keys available.");
AddAction(
   1,
   af_none,
   "Simulate keyup",
   "Keyboard Simulation",
   "Simulate keyup {0}",
   "Simulate a keyup from the keyboard",
   "send_keyup"
);


Please, read the comments, they will explain the parameters for the AddAction function(the AddKeyParam function i just copied and pasted from the keyboard plugin conditions section(you may want to take a look there))

Implementing the actions
Open the second file and find this line:

Code: Select all
   function Acts() {};


right under it you paste this code:

Code: Select all
   Acts.prototype.send_keydown = function(key){
      jQuery.event.trigger({ type : 'keydown', which : key });
   }

   Acts.prototype.send_keyup = function(key){
      jQuery.event.trigger({ type : 'keyup', which : key });
   }


If you know some programming you will understand everything easily.

Now you can save and close the two files, when you open construct2 now the keyboard plugin have the two actions: keyup and keydown under the sections Keyboard Simulation, just use them.

The exact same thing can be done for the gamepad, it may be more complicated because simulate gamepad buttons with javascript may be hard(or no).
B
5
S
2
Posts: 8
Reputation: 443

Post » Fri Jun 23, 2017 6:32 am

PauloMarcio wrote:Ok, i have edited the keyboard plugin and here comes a little tutorial:


Wow! Thank you! This is very in depth. I will try this out later tonight and see what I come up with.
B
12
S
4
G
2
Posts: 132
Reputation: 1,749


Return to How do I....?

Who is online

Users browsing this forum: No registered users and 13 guests