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.
Results 1 to 5 of 5
  1. #1
    skl
    skl is offline
    New to the CF scene
    Join Date
    Sep 2005
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    How to detect Ctrl key pressed?

    Hi,

    I have no problem in detecting the CTRL key with the javascript code event.keyCode or event.ctrlKey call in my script function. But once I called the setInterval() (e.g. <body onLoad='setInterval("poll()", 1000)'>) then the event is always "null". Please help.

    Note: poll() is my javascript method where I would like to detect the CTRL key pressed.

    Best Regards
    skl

  • #2
    TNO
    TNO is offline
    Regular Coder
    Join Date
    Apr 2005
    Posts
    213
    Thanks
    2
    Thanked 1 Time in 1 Post
    Have you tried the following?

    Code:
    onkeydown="if (event.keyCode == 17){blah blah}"

  • #3
    skl
    skl is offline
    New to the CF scene
    Join Date
    Sep 2005
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Yes, it work as:

    <body onkeydown='if (event.keyCode == 17){window.status="Blah Blah"}' onLoad="startPolling()"> ... </body>

    But, the problem I am having is I have to check the keyCode in my javascript functions and do not know how to do it, for example:

    function startPolling()
    {
    setInterval("poll()", 1000)
    }
    function poll()
    {
    window.status = event.ctrlKey; // or event.keycode
    ...
    }

    BTW, I do not use FORM in my page.

    Best Regards,
    skl

  • #4
    TNO
    TNO is offline
    Regular Coder
    Join Date
    Apr 2005
    Posts
    213
    Thanks
    2
    Thanked 1 Time in 1 Post
    Maybe something like this:

    <script language="javascript">
    function handleKeyDown(evt){
    if(evt.keyCode==17){
    ---DoThis---
    }
    if(evt.keyCode==24){
    ---DoThat---
    }

    }
    </script>

    <body onkeydown="handleKeyDown(window.event);">

  • #5
    skl
    skl is offline
    New to the CF scene
    Join Date
    Sep 2005
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    It works, thanks a lot.


  •  

    Posting Permissions

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