Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 5 of 5
  1. #1
    Junior Member
    Join Date
    Apr 2006
    Location
    Near Keighley Yorkshire UK
    Posts
    12
    Member #
    13087
    Hi has any body any idea why this only works for click and double-click but not for key presses? It is the same in Netscape, Firefox and Internet Explorer 7 and does not work at all in Safari. It could be any key a-z, space bar.
    //
    // Capture events click, dblclick and keypress and link depending on rule in handleEvents()
    //
    document.captureEvents(Event.CLICK);
    document.captureEvents(Event.KEYPRESS);
    document.captureEvents(Event.DBCLICK);
    document.onclick=handleEvents;
    document.ondblclick=handleEvents;
    document.onKeyPress=handleEvents;
    if(!document.all || document.layers){
    document.captureEvents(Event.CLICK);
    document.captureEvents(Event.KEYPRESS);
    document.captureEvents(Event.DBCLICK);
    }
    Naturally there is a function called handleEvents() this just decides on the link to take next.

  2.  

  3. #2
    WDF Staff smoseley's Avatar
    Join Date
    Mar 2003
    Location
    Boston, MA
    Posts
    9,729
    Member #
    819
    Liked
    205 times
    I did some work and came up with this:
    Code:
    <html>
    	<head>
    		<title></title>
    		<script language="javascript">
    			if (window.addEventListener) {
    				window.addEventListener('keypress', handleKeyPress, true);
    				window.addEventListener('click', handleClick, false);
    				window.addEventListener('doubleclick', handleDoubleClick, false);
    			} else {
    				document.onclick = handleClick;
    				document.ondblclick = handleDoubleClick;
    				document.onKeyPress = handleKeyPress;
    				document.onKeyUp = handleKeyPress;
    				if(document.layers){
    					document.captureEvents(Event.CLICK);
    					document.captureEvents(Event.KEYPRESS);
    					document.captureEvents(Event.DBCLICK);
    				}
    
    			}
    			function handleClick() {
    				window.alert("click");
    			}
    			function handleKeyPress(e) {
    				var key;
    				if (document.layers)
    					key = e.which;
    				else
    					if (window.event) {
    						key = window.event.keyCode;
    					} else {
    						key = e.keyCode;
    					}
    				if (key > 0) {
    
    					alert(key);
    				}
    			}
    		</script>
    	</head>
    	<body>
    	</body>
    </html>
    It works with all keys except alphanumeric. Not sure what's going on with that... needs more research.

  4. #3
    Senior Member Steax's Avatar
    Join Date
    Dec 2006
    Location
    Bandung, Indonesia
    Posts
    1,207
    Member #
    14572
    Since I've been on a crusade about jQuery for a while, take a look at the js-hotkeys plugin for jQuery. It should do what you want much easier.
    Note on code: If I give code, please note that it is simply sample code to demonstrate an effect. It is not meant to be used as-is; that is the programmer's job. I am not responsible to give you support or be held liable for anything that happens when using my code.

  5. #4
    Junior Member
    Join Date
    Apr 2006
    Location
    Near Keighley Yorkshire UK
    Posts
    12
    Member #
    13087
    Hi Thanks must go and to work just now - I will try this later.

  6. #5
    Junior Member
    Join Date
    Apr 2006
    Location
    Near Keighley Yorkshire UK
    Posts
    12
    Member #
    13087
    Hi
    I got it working I put in on my website for my students
    http://www.weblark.net/WebDesign/Jav...t/Page0903.htm for the code and click the link at bottom of page to try it out.
    Mainly a problem with upper and lowercase code.
    I used the listener code from Steven (thank you for that).
    Next more to learn from Rizqi (sorry about the table layouts!) but it keeps things together nicely
    PS I know my site needs some attention regarding navigation and I am trying to give up tables.
    Pat


Remove Ads

Posting Permissions

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

Search tags for this page

javascript onkeypress handleevent

Click on a term to search for related topics.
All times are GMT -6. The time now is 10:33 PM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com