...

View Full Version : Java script for Keypress and the script to work



aravitejareddy
09-04-2010, 10:03 PM
I need to assign a key in the javascript to actually make the javascript work,.

I have a bookmark in chrome , a javascript , which actually works when clicked on it .,. but how can i edit it so that i can actually make it work on click a key or combination of keys.

i want to declare the key or keycombo in the script itself .,.

the script is for catching the selected text on the webpage and opening a new tab(or window) and doing an exact search search of the selected text using google.com .,.,

So I want it to work it this way .,

select the text
press a key
and it opens a new tab (or window) with an xact search .,.
i want to declare the key or keycombo in the script itself .,.

the script is for catching the selected text on the webpage and opening a new tab(or window) and doing an exact search search of the selected text using google.com .,.,

So I want it to work it this way .,

select the text
press a key
and it opens a new tab (or window) with an xact search .,.


Thanks in advance .,

Nani

Philip M
09-05-2010, 10:47 AM
I am not sure that I understand, but you can capture a keypress so:-


<script type = "text/javascript">

document.onkeydown = function(ev) {
var key;
ev = ev || event;
key = ev.keyCode;
alert ("Keycode = " + key);

if (key == 13) {
alert ("You pressed the ENTER key");
}

if( ev.ctrlKey || ev.shiftKey ) {
alert ("You pressed the Control key or the Shift Key");
}

which = String.fromCharCode(key).toUpperCase();
if (key>=48 && key<=90) {
alert ("You pressed the " + which + " key");
}

}
</script>

But there is no way to capture a keypress combinatiion such as CTL-A.

Quizmaster: For what type of triangle might one use Pythagoras's theorem?
Contestant: One with three points.

aravitejareddy
09-05-2010, 01:39 PM
Thanks for your reply for my question

I want the script to be used as a bookmarklet or an extension .,. since an extension is lil hard a bookmarklet with the javascript is fine .,.

My Javascript bookmarklet already does this thing

When text is selected and clicked on the bookmarklet it opens a new tab and does an exact search with the snippet of the text selected using google.com/search?q="(myselected text)"

Now i want it to do automatically on a key press

When certain text is selected and selected key is pressed it should do the work as when i clicked on the bookmarklet.

Note: I'm using this bookmarklet in CHROME

my javascript is
javascript:a = "" + (window.getSelection() ? document.getSelection() : document.selection.createRange().text); if (a!=null)window.open("http://www.google.com/search?q=\"" + escape(a)+ "\"");

Philip M
09-05-2010, 04:12 PM
Sorry, I have no experience of Chrome.

Try placing this in a function


function useKey() {
var a = "" + (window.getSelection() ? document.getSelection() : document.selection.createRange().text);
if (a!=null)window.open("http://www.google.com/search?q=\"" + escape(a)+ "\"");
}
and call the function when the selected key is pressed (see code given above in Post#2).

rnd me
09-05-2010, 09:10 PM
and call the function when the selected key is pressed (see code given above in Post#2).

note that many keys without "letter values" (return, left, etc) don't fire the keyPress event, they only fire keyDown and keyUp. I would imagine that would be a consideration for this type of app.

Philip M
09-05-2010, 10:31 PM
note that many keys without "letter values" (return, left, etc) don't fire the keyPress event, they only fire keyDown and keyUp. I would imagine that would be a consideration for this type of app.

Yes, that is so, but my code reads

document.onkeydown = function(ev) {

:p

tz22
10-19-2010, 08:26 PM
<script type = "text/javascript">
var ctl = ''
var alt = ''
var s = ''
document.onkeydown = function(ev) {
var key;
ev = ev || event;
key = ev.keyCode;
if (key == 17) {
ctl = 1
}
if (key == 18) {
alt = 1
}
if (key == 83) {
s = 1
}
if(ctl+alt+s==3){
window.open('http://www.google.com');
ctl = ''
alt = ''
s = ''
}
}
document.onkeyup = function() {
ctl = ''
alt = ''
s = ''
}
</script>

Here is a way to use a key combo.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum