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
Last edited by aravitejareddy; 09-05-2010 at 07:56 AM..
Reason: clarity
I am not sure that I understand, but you can capture a keypress so:-
Code:
<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.
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)+ "\"");
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).
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.
__________________ my site (updated 5/13) STATS (2013/5) HTML5:90.2% MOB:14% IE7:0.5% IE8:8.6% IE9:9.8% IE10:10%
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.
<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>