How to handle which keys are pressed

Posted on 2006-04-18
Last Modified: 2006-11-18
This is an easy one i think. I have a form and want to be able to recognize which key was pressed. I've read that there are differences between browsers so i just need it to work in firefox (although it would be interesting to know the difference to IE).

In fact i just want to stop the user from pressing the key "comma" (,) when inside an input field. I read a lot and finally never understood how to make it work. OnKeyUp ? onKeyDown ? onKeyPress ?

Thanks a lot
Question by:mrxcol

    Accepted Solution

    <script type="text/javascript">
    function noCommas(e)
    var keynum

    if(window.event) // IE
    keynum = e.keyCode
    else if(e.which) // Netscape/Firefox/Opera
    keynum = e.which
    return !(keynum==188)

    <input type="text" onkeydown="return noCommas(event)" />


    this works in firefox ie the only thing i cant figure out is how to tell if the capslock or shift key
    was pushed first. this will keep commas from being entered but also the less than symbol.
    LVL 1

    Author Comment

    Great, thanks !

    Do you know a good site where i can find a detailed explanation about DCOM, Gecko, COM, W3C thing ? I stiull haven't managed to understand it enough.

    What was driving me crazy was that window.event and e.which thing. Thanks a lot !

    Expert Comment


    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    Both jQuery and Mootools offer some nice, easy to use scripts. Mootools is best known for their interactive slideshows and jQuery for their menus, but both have many other uses as well.   jQuery is basically a toolkit with many DOM (http://en.wik…
    JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
    The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
    The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now