Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 24

Thread: Pressing a key

  1. #1
    New to the CF scene
    Join Date
    Jul 2011
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post Pressing a key

    How can I simulate pressing a key in javascript into a specific textfield?

  • #2
    New Coder
    Join Date
    Apr 2011
    Location
    Internet
    Posts
    33
    Thanks
    0
    Thanked 0 Times in 0 Posts
    <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.
    Environment friendly mosquito nets

  • #3
    New to the CF scene
    Join Date
    Jul 2011
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by cyprusholidays View Post
    <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

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,147
    Thanks
    203
    Thanked 2,547 Times in 2,525 Posts
    Quote Originally Posted by ionutcib View Post
    ...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:-


    Code:
    <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
    Last edited by Philip M; 07-05-2011 at 10:32 AM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #5
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    By short, you can't. Either you can write a text, or you can capture a key stroke.
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #6
    New to the CF scene
    Join Date
    Jul 2011
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #7
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,147
    Thanks
    203
    Thanked 2,547 Times in 2,525 Posts
    Quote Originally Posted by ionutcib View Post
    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
    Code:
    <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>

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #8
    New to the CF scene
    Join Date
    Jul 2011
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Philip M View Post
    Code:
    <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

  • #9
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,147
    Thanks
    203
    Thanked 2,547 Times in 2,525 Posts
    Quote Originally Posted by ionutcib View Post
    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. What is it which is supposed to "simulate" a key press?

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

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #10
    New to the CF scene
    Join Date
    Jul 2011
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Philip M View Post
    I am sorry, but I do not understand you at all. 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
    Last edited by ionutcib; 07-05-2011 at 04:48 PM.

  • #11
    New to the CF scene
    Join Date
    Jul 2011
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    The page must not contain the javascript code BUT the javascript will be executed after the page is loaded

  • #12
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,147
    Thanks
    203
    Thanked 2,547 Times in 2,525 Posts
    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?)
    Last edited by Philip M; 07-05-2011 at 08:53 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #13
    New to the CF scene
    Join Date
    Jul 2011
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Philip M View Post
    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/simula...eypress-events

  • #14
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,147
    Thanks
    203
    Thanked 2,547 Times in 2,525 Posts
    Quote Originally Posted by ionutcib View Post
    You have a point...but as I have said...it seems that this is possible with jQuery:
    http://forum.jquery.com/topic/simula...eypress-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.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #15
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    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?
    Last edited by Kor; 07-06-2011 at 09:42 AM.
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •