-2

I am not looking for how to trigger event, I am looking for way how to simulate key press action.

I am looking for ACTION not EVENT...

This code is not working for me:

element.trigger('focus').trigger({ type : 'keydown', which : 50 })

Because when user enter something into the input much more things happend, e.g. events(keypress, keyup, keydown), character is entered into input , some DOM events are triggered etc.

With this code:

element.trigger({ type : 'keydown', which : 50 })

input stays empty. I know I can use :

element.trigger({ type : 'keydown', which : 50 }).val(2);

but I am looking for more complete solution, simulate action if it is possible.

Any suggestion?

Ashish Bahl
  • 1,482
  • 1
  • 18
  • 27
fico7489
  • 7,931
  • 7
  • 55
  • 89
  • My question is _why_. Why do you exactly need to simulate user activity? – hindmost Feb 02 '17 at 09:10
  • @hindmost why reason is important ? I want to know how to do this in javascript this is why stackoverflow exists to ask questions.... – fico7489 Feb 02 '17 at 09:15
  • @hindmost some plugins can register many events on input and I want to simulate user keypress action. I want to plugin do what need to do when user press key.... – fico7489 Feb 02 '17 at 09:16
  • Your question in its present form is off-topic as _too broad_ since it's unclear what exactly events/actions you want to trigger/simulate. Add details to narrow the answer – hindmost Feb 02 '17 at 09:22
  • @hindmost who said I want trigger events ? I want trigger ACTION not EVENT... – fico7489 Feb 02 '17 at 09:23
  • Possible duplicate of [Get event listeners attached to node using addEventListener](http://stackoverflow.com/questions/9046741/get-event-listeners-attached-to-node-using-addeventlistener) – zurfyx Feb 02 '17 at 09:56
  • @zurfyx it is not duplicate, I am not looking for events.... – fico7489 Feb 02 '17 at 09:58

3 Answers3

0

jQuery offers a built-in solution for this.

.keypress

$( "#target" ).keypress();
$( "#target" ).keypress(function() {
  console.log( "Handler for .keypress() called." );
});

.keyup

$('#target').keyup();
$('#target').keyup(function() {
    console.log( "Handler for .keyup() called." );
});

.keydown

$('#target').keydown();
$('#target').keydown(function() {
    console.log( "Handler for .keydown() called." );
});

Edit:

So you are trying to list event listeners? See Get event listeners attached to node using addEventListener

The only way to get a list of all event listeners attached to a node is to intercept the listener attachment call.

Community
  • 1
  • 1
zurfyx
  • 31,043
  • 20
  • 111
  • 145
  • but when user performs keypress action more events are triggered as I wrote (keypress, keyup, keydown) etc.... – fico7489 Feb 02 '17 at 09:12
  • You did not read my question, + some DOM events are triggered, + character is entered into the input, + how I can be sure what actions and events are triggered when user preforms keypress action ? when user performs keypress ACTION more than 10 things happends, not just keyup, keypress and keydown events.... – fico7489 Feb 02 '17 at 09:20
  • still not good answer, keypress action can even move other html elements and trigger x event on x elements.... – fico7489 Feb 02 '17 at 09:52
  • I know how to simulate and trigger events but I want simulate physically keyboard keypress action....I am not talking about events.... – fico7489 Feb 02 '17 at 09:57
  • @fico7489 `keypress`, `keyup`, `keydown` is all you can do (with jQuery, or similar Javascript ones). They will trigger 0 to n listeners, because that is what listeners are for. The keyboard drivers cannot be touched directly, that would be a huge security risk. – zurfyx Feb 02 '17 at 10:05
0

I guess you would like to simulate event of specific keyPress event.

In such case you can use jQuery jQuery.Event Constructor

I hope you are trying to simulate number 2 key. In such case you can do like:

var e = jQuery.Event( "keypress" );
e.which = 50;

$("#some_element").trigger( e );

Edit: I'm just trying to help. Coming to the point if you want to generate a action after that event you can try below.

Register your action/event

$("#some_element").keyPress( function( e ){
   // the action to perform when keyPress event raised.
} );
Ravi MCA
  • 2,491
  • 4
  • 20
  • 30
0

I doubt if you are going to find a proper answer to you problem here. As mentioned earlier we're lacking context to give you the best possible solution, however I can try.

am looking for ACTION not EVENT...

You can't control the users computer, the closest thing to this is calling an event. So yes, you might be looking for an event.

Because when user enter something into the input much more things happend, e.g. events(keypress, keyup, keydown), character is entered into input , some DOM events are triggered etc.

The events can be simulated, appending characters can be simulated, DOM events can be simulated. However, the user or the users device cannot be simulated.

You probably are going to need to write some abstractions. Find out what is currently focussed, parse the stuff that can be applicable to that and simulate the behavior. Perhaps its also possible to do some low level intercepting of addEventListener calls to proxy them through your own code.

What I'd suggest is find out if this is your real problem and to make sure your'e not facing an XY problem. For instance, why do you want to simulate the user, why are you not separating the "wiring" from the "logic".

For security reasons, access to the browser is limited, so not everything you want to do might be possible, however is this for instance is for integration testing you might be able to use something like Selenium instead which allows you to do what you asked but only on the machine it's executed from.

Sven van de Scheur
  • 1,809
  • 2
  • 15
  • 31