...

View Full Version : Pressing a key



ionutcib
07-05-2011, 08:57 AM
How can I simulate pressing a key in javascript into a specific textfield?

cyprusholidays
07-05-2011, 09:33 AM
<input type="checkbox" onClick="document.getElementById('theSubmitButton').click();">Check the box to simulate a button click
<br>
<input type="button" name="theSubmitButton" id="theSubmitButton" value="Button" onClick="alert('The button was clicked.');">

I found this code on my friends website and hope this helps to fix your need.

ionutcib
07-05-2011, 09:52 AM
<input type="checkbox" onClick="document.getElementById('theSubmitButton').click();">Check the box to simulate a button click
<br>
<input type="button" name="theSubmitButton" id="theSubmitButton" value="Button" onClick="alert('The button was clicked.');">

I found this code on my friends website and hope this helps to fix your need.

No...That does not help...I want to simulate pressing a keyboard key(like space) not a button

Philip M
07-05-2011, 10:27 AM
...I want to simulate pressing a keyboard key(like space) not a button
I don't really understand what you mean, but this may help:-



<input type = "text" id = "txtfld">

<script type = "text/javascript">

document.onkeydown = function(ev) {
var key;
ev = ev || event;
key = ev.keyCode;
if (key == 32) { // space key
document.getElementById("txtfld").value += " Some Added Text"
}
}

</script>


Groundhogs are indigenous on this man-made island. - BBC3 Nature program

Kor
07-05-2011, 10:29 AM
By short, you can't. Either you can write a text, or you can capture a key stroke.

ionutcib
07-05-2011, 12:57 PM
I have found that what I want could be done with jquery...which is also javscript...so I think it could be done...the solution provided by Philip M is not good for me because it implies changing the page content...and I want just to execute some javascript on a loaded page

Philip M
07-05-2011, 03:32 PM
I have found that what I want could be done with jquery...which is also javscript...so I think it could be done...the solution provided by Philip M is not good for me because it implies changing the page content...and I want just to execute some javascript on a loaded page


<script type = "text/javascript">

document.onkeydown = function(ev) {
var key;
ev = ev || event;
key = ev.keyCode;
if (key == 32) { // space key
// execute some Javascript function
}
}

</script>

ionutcib
07-05-2011, 04:16 PM
<script type = "text/javascript">

document.onkeydown = function(ev) {
var key;
ev = ev || event;
key = ev.keyCode;
if (key == 32) { // space key
// execute some Javascript function
}
}

</script>

I'm not a javascript expert but I think the function above runs when the space key is pressed. What I need is the key to be pressed(virtually). So I need a simulation of key press. I don't care what's happening if the key is pressed but I just want it pressed virtually.
Hope that will clarify you :)

Philip M
07-05-2011, 04:25 PM
I'm not a javascript expert but I think the function above runs when the space key is pressed. What I need is the key to be pressed(virtually). So I need a simulation of key press. I don't care what's happening if the key is pressed but I just want it pressed virtually.
Hope that will clarify you :)

I am sorry, but I do not understand you at all. :confused: What is it which is supposed to "simulate" a key press?

All you can do is capture a keystroke, and then invoke some function.

ionutcib
07-05-2011, 04:45 PM
I am sorry, but I do not understand you at all. :confused: What is it which is supposed to "simulate" a key press?

All you can do is capture a keystroke, and then invoke some function.

Google this: "simulate keypress"

So...after the page is loaded it should react when JAVASCRIPT code is executed like I press space key although I didn't pressed it. I need that JAVASCRIPT code.

Thank you

ionutcib
07-05-2011, 04:55 PM
The page must not contain the javascript code BUT the javascript will be executed after the page is loaded

Philip M
07-05-2011, 08:50 PM
You do not give any reason or explanation for what you are trying to do. Is it some attempt to make the user view some advert involuntarily?

All I know is that I hope it's impossible as Kor says. I don't like the idea that a script running in my browser could simulate keystrokes. (Ctrl-Alt-Del?)

ionutcib
07-05-2011, 09:03 PM
You do not give any reason or explanation for what you are trying to do. Is it some attempt to make the user view some advert involuntarily?

All I know is that I hope it's impossible as Kor says. I don't like the idea that a script running in my browser could simulate keystrokes. (Ctrl-Alt-Del?)

You have a point...but as I have said...it seems that this is possible with jQuery:
http://forum.jquery.com/topic/simulating-keypress-events

Philip M
07-06-2011, 08:21 AM
You have a point...but as I have said...it seems that this is possible with jQuery:
http://forum.jquery.com/topic/simulating-keypress-events

So use it then. I note that you have not explained why you want to do this, which leads me to suspect your motive.

Kor
07-06-2011, 09:39 AM
JQuery is a JavaScript structured library, thus JQuery is JavaScript, so that it can not do more than the base language, JavaScript, can do.

The expression "simulate a keystroke" is used in an inadequate way, when it comes about JavaScript. I repeat: what JavaScript can do is either to capture a key stroke (and not all of them, and not in every combination) or to perform some of the actions those keys stroke might do, but only within the browser, for instance writing letters, scrolling the document, going back to a previous page... things like that. For that, JavaScript uses its methods. Those methods do not cover all the actions keys stroke can do, thus there are actions (like closing the browser) which JavaScript can not simulate.

Furthermore: different browsers are able to capture or not different keys or key combinations. There are incomplete implementations or even bugs in different browsers and even in different versions of the same browser.

One more thing: even if the keystrokes are captured, there are some essential combinations (ALT+F4, ALT+CTRL+DEL, and so on) which can not be blocked, because they "belong" to the Operating System, not to the browser.

Regarding the SPACE key. Within a browser, pressing the SPACE key can do two different actions, according to the focus. If the document is focused, SPACE will scroll down the page. If a form's input control (a text field, a textarea...) SPACE will insert a blank space. So, which of those two actions do you want to "simulate". Both?

ionutcib
07-06-2011, 10:41 AM
I want this action:
If a form's input control (a text field, a textarea...) SPACE will insert a blank space.

Thank you (multumesc :) )

Kor
07-06-2011, 11:03 AM
I want this action:
If a form's input control (a text field, a textarea...) SPACE will insert a blank space.

Thank you (multumesc :) )
But that is what the SPACE key does by default. Or you want to build a virtual keyboard? And when the user presses a button, a blank space will be inserted. Is this what you want?

ionutcib
07-06-2011, 11:09 AM
But that is what the SPACE key does by default. Or you want to build a virtual keyboard? And when the user presses a button, a blank space will be inserted. Is this what you want?
I want to get the content of a page with my application after I automatically submit some values...the page does not show the information unless you press a key...setting the textfields values with ".value = " is not good...this is somewhat like a protection.

Kor
07-06-2011, 11:33 AM
Confusing. I don't understand what do you want. Please, can you re-form your aim and detail it in common but accurate words and terms?

LSCare
07-06-2011, 01:43 PM
I want to get the content of a page with my application after I automatically submit some values...the page does not show the information unless you press a key...setting the textfields values with ".value = " is not good...this is somewhat like a protection.

I've read through and made about as much sense of it as anyone else who has attempted to respond.

All i have to add is...

If you want an action to occur when you press something... Use a Button!

If you want something to automatically happen... Can it be done using a time delayed event.

Kor
07-06-2011, 01:53 PM
I think I understood, after a serious thought :)

ionutcib has an application (probably an AJAX request + proxy) which can grab the content of a document belonging to a third part (on another domain). His problem is that content is visible in a way or another on that third part page only if the user presses a key. That feature is, probably done via a JavaScript code written on that third part document.

If so, the answer is definitely: ionutcib, you can not do that. There is no way to call a foreign JavaScript function written on another domain.

Maybe that function there launch his own AJAX request. In this case you might instruct your proxy to perform the request to the AJAX url instead of the document's url... But that is a presumption, thus I don't think we might have other solution without seeing all the codes: yours and theirs.

devnull69
07-06-2011, 01:54 PM
As far as I understand: There is an event handler on the page that obviously "somehow" already captures the SPACE key and performs "some action". And "somehow" ionutcib wants to automatically trigger that action.

I think he/she should take a closer look at the "Events" tabs when using Firebug with the Eventbug extension. There is no general answer without a URL or complete code.

Philip M
07-06-2011, 04:46 PM
I think I understood, after a serious thought :)

ionutcib has an application (probably an AJAX request + proxy) which can grab the content of a document belonging to a third part (on another domain).

Surely that is a breach of copyright and guaranteed to attract a swarm of expensive lawyers. I think we can now understand why the OP is so coy about explaining his objective.

Kor
07-06-2011, 08:32 PM
Surely that is a breach of copyright and guaranteed to attract a swarm of expensive lawyers. I think we can now understand why the OP is so coy about explaining his objective.
I was afraid of that, as well :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum