Log in

View Full Version : Override default key ...



shachi
06-18-2006, 09:09 AM
Is there any way to override default key events like Ctrl+A or Ctrl+P like in this website: http://www.jgraph.com/mxgraph/ (hit ctrl+A to see the event)?? It does not select the text in a normal way(like when you select a text the background turns bluish). If anyone know how this can be done then I would be appreciated with your help.

Twey
06-18-2006, 09:47 AM
If you return false from an onkeypress event, the normal action will not be performed.

shachi
06-18-2006, 09:56 AM
Sorry but I didn't get it.:confused: Ctrl+A is not a default keypress and how do I return a keypress false?? Please can you make this clear for me??

djr33
06-18-2006, 10:08 AM
1. This will fail on macs.
2. While Twey did give you a way... (see below for an example), the main point here is that you seem to be trying to stop people from saving your content, etc. If this is the case GIVE UP, because it simply cannot be done and goes against all the standards of the web. Not only is it not possible, but if you try, you'll just end up with a heavily coded page that will confuse your normal visitors.


As for the return false thing... if you didn't want a link to work, you could try something like this:
<a href="whatever.htm" onClick="return false;">
And it would do nothing.
*I think this is the right code, but might be slightly off.
Basically.... just use "return false;" in place of where javascript to be executed would go, and this will instead make it not execute.

shachi
06-18-2006, 10:15 AM
2. While Twey did give you a way... (see below for an example), the main point here is that you seem to be trying to stop people from saving your content, etc. If this is the case GIVE UP, because it simply cannot be done and goes against all the standards of the web. Not only is it not possible, but if you try, you'll just end up with a heavily coded page that will confuse your normal visitors.


Actually I am not doing this to stop people from saving my content but I had made a script to increase and decrease textboxes(just like this one) and I wanted it to increase when the user hits Ctrl+I and decrease when he hits Ctrl+D but in firefox and some other browsers this didn't seem to work as those events are already registered for Page Info and Bookmarks so I wanted to override those events. But I don't have a clue how I can do it so I came across that site and knew that it was what I wanted and I seeked for your help.

But still I don't have any idea how it can be done. Thanks anyways.

djr33
06-18-2006, 10:17 AM
Ah, ok. Then the issue here is that you might not have complete compatibility, and you wouldn't want someone with a weird browser bookmarking your site instead of changing text size, or whatever.

I'd suggest using Alt+Letter instead. That's the standard way of doing web-based user input from the keyboard.

It'll save you a lot of time as well.

Twey
06-18-2006, 10:23 AM
Tutorial: Get the Key Press Event Using Javascript (http://www.geekpedia.com/tutorial138_Get-key-press-event-using-JavaScript.html)

shachi
06-18-2006, 10:25 AM
Thanks Twey, drj33. Let me see the link Twey gave me and if I still have problems then I am afraid I need to bother again.:(

Unlucky me!

djr33
06-18-2006, 10:28 AM
Sure.

Just remember the alt way will be easier, if not as automatic for the user.

Even this forum, when making a post (in the editing mode, not the quick reply mode) has Alt+S=post, etc.

shachi
06-18-2006, 10:40 AM
Yes I can use the Alt key using the accesskey simply and it would also be easier for me but the one whose website I am making says it needs to be Ctrl and I am tired of convincing him about it. :(

By the way Twey I read that article and I it doesn't seem to be working.
Here's the code.


<script type="text/javascript">

document.onkeydown = keypress;


function keypress(e){

var key_s = (window.event) ? event.keyCode : e.keyCode;
if(key_s == 17 && 65){
alert("This event is supposed to show an alert AND not select the text");
return false;
}
}
</script>
<body>
bla lba blalklddjkfjkdfsjkldfsjkldfsjkldfsjl<br>
bla lba blalklddjkfjkdfsjkldfsjkldfsjkldfsjl<br>
bla lba blalklddjkfjkdfsjkldfsjkldfsjkldfsjl<br>
bla lba blalklddjkfjkdfsjkldfsjkldfsjkldfsjl<br>
bla lba blalklddjkfjkdfsjkldfsjkldfsjkldfsjl<br>
bla lba blalklddjkfjkdfsjkldfsjkldfsjkldfsjl<br>
bla lba blalklddjkfjkdfsjkldfsjkldfsjkldfsjl<br>
bla lba blalklddjkfjkdfsjkldfsjkldfsjkldfsjl<br>
bla lba blalklddjkfjkdfsjkldfsjkldfsjkldfsjl<br>
bla lba blalklddjkfjkdfsjkldfsjkldfsjkldfsjl<br>
bla lba blalklddjkfjkdfsjkldfsjkldfsjkldfsjl<br>
bla lba blalklddjkfjkdfsjkldfsjkldfsjkldfsjl<br>
bla lba blalklddjkfjkdfsjkldfsjkldfsjkldfsjl<br>
bla lba blalklddjkfjkdfsjkldfsjkldfsjkldfsjl<br>
bla lba blalklddjkfjkdfsjkldfsjkldfsjkldfsjl<br>
bla lba blalklddjkfjkdfsjkldfsjkldfsjkldfsjl<br>
bla lba blalklddjkfjkdfsjkldfsjkldfsjkldfsjl<br>
bla lba blalklddjkfjkdfsjkldfsjkldfsjkldfsjl<br>
bla lba blalklddjkfjkdfsjkldfsjkldfsjkldfsjl<br>
bla lba blalklddjkfjkdfsjkldfsjkldfsjkldfsjl<br>
bla lba blalklddjkfjkdfsjkldfsjkldfsjkldfsjl<br>
bla lba blalklddjkfjkdfsjkldfsjkldfsjkldfsjl<br>
bla lba blalklddjkfjkdfsjkldfsjkldfsjkldfsjl<br>
bla lba blalklddjkfjkdfsjkldfsjkldfsjkldfsjl<br>
bla lba blalklddjkfjkdfsjkldfsjkldfsjkldfsjl<br>
bla lba blalklddjkfjkdfsjkldfsjkldfsjkldfsjl<br>
bla lba blalklddjkfjkdfsjkldfsjkldfsjkldfsjl<br>
bla lba blalklddjkfjkdfsjkldfsjkldfsjkldfsjl<br>
bla lba blalklddjkfjkdfsjkldfsjkldfsjkldfsjl<br>
bla lba blalklddjkfjkdfsjkldfsjkldfsjkldfsjl<br>
bla lba blalklddjkfjkdfsjkldfsjkldfsjkldfsjl<br>
bla lba blalklddjkfjkdfsjkldfsjkldfsjkldfsjl<br>
bla lba blalklddjkfjkdfsjkldfsjkldfsjkldfsjl<br>
</body>



There are two problems in this. First that the alert comes up as soon as I press Ctrl and another is that the text gets selected.

I'd be very grateful if anyone can tell me the problem and If nothing works then I must go to the accesskey method.

Thanks everybody.

Twey
06-18-2006, 10:46 AM
Using accesskeys is actually a much better method, since it doesn't require scripting support.

djr33
06-18-2006, 10:46 AM
If you know more about web design than he does, you simply need to tell him he's wrong. Do it in a nice way, but sometimes you just need to tell someone it won't work.

You could ask him this:
Would you rather have it be control and work most of the time, or have it be alt and work all the time.

If you do decide to go with control, do it only if you just want it to work a good bit of the time, rather than needing it to work all the time.

If you're relying on this as a crucial function, then I'd definitely rethink it.

You do html <html></html> tags around that, right? Not sure about the function... this isn't really my thing. I'm just trying to help point you in the right direction.

shachi
06-18-2006, 10:49 AM
drj33 Thanks for the advise. I applied the accesskey to the thing and it is working and it's allright now but still I really really want to know if something like that could be done.

Thanks for all your help everyone.:)

Cheers!!

djr33
06-18-2006, 10:58 AM
Hmm... would you consider using both? At least if ctrl failed, you'd still have alt working.

shachi
06-18-2006, 11:01 AM
Yea sure why not.

shachi
06-18-2006, 11:06 AM
Uh-oh Just found out accesskey is not working in IE. :(