Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-04-2010, 10:03 PM   PM User | #1
aravitejareddy
New to the CF scene

 
Join Date: Sep 2010
Posts: 2
Thanks: 1
Thanked 0 Times in 0 Posts
aravitejareddy is an unknown quantity at this point
Java script for Keypress and the script to work

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
aravitejareddy is offline   Reply With Quote
Old 09-05-2010, 10:47 AM   PM User | #2
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,033
Thanks: 197
Thanked 2,410 Times in 2,388 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
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.
Philip M is offline   Reply With Quote
Users who have thanked Philip M for this post:
aravitejareddy (09-05-2010)
Old 09-05-2010, 01:39 PM   PM User | #3
aravitejareddy
New to the CF scene

 
Join Date: Sep 2010
Posts: 2
Thanks: 1
Thanked 0 Times in 0 Posts
aravitejareddy is an unknown quantity at this point
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)+ "\"");
aravitejareddy is offline   Reply With Quote
Old 09-05-2010, 04:12 PM   PM User | #4
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,033
Thanks: 197
Thanked 2,410 Times in 2,388 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Sorry, I have no experience of Chrome.

Try placing this in a function

Code:
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).

Last edited by Philip M; 09-05-2010 at 04:17 PM..
Philip M is offline   Reply With Quote
Old 09-05-2010, 09:10 PM   PM User | #5
rnd me
Senior Coder

 
rnd me's Avatar
 
Join Date: Jun 2007
Location: Urbana
Posts: 3,454
Thanks: 9
Thanked 466 Times in 450 Posts
rnd me is a jewel in the roughrnd me is a jewel in the roughrnd me is a jewel in the rough
Quote:
Originally Posted by Philip M View Post
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%
rnd me is offline   Reply With Quote
Old 09-05-2010, 10:31 PM   PM User | #6
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,033
Thanks: 197
Thanked 2,410 Times in 2,388 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Quote:
Originally Posted by rnd me View Post
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) {

Philip M is offline   Reply With Quote
Old 10-19-2010, 08:26 PM   PM User | #7
tz22
New to the CF scene

 
Join Date: Oct 2010
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
tz22 is an unknown quantity at this point
Code:
<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.
tz22 is offline   Reply With Quote
Reply

Bookmarks

Tags
event, javascript, keys, shortcuts

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:51 PM.


Advertisement
Log in to turn off these ads.