Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New to the CF scene
    Join Date
    Sep 2010
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    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

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,928
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    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.

  • Users who have thanked Philip M for this post:

    aravitejareddy (09-05-2010)

  • #3
    New to the CF scene
    Join Date
    Sep 2010
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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)+ "\"");

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,928
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    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.

  • #5
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,302
    Thanks
    10
    Thanked 586 Times in 567 Posts
    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 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,928
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    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) {


  • #7
    New to the CF scene
    Join Date
    Oct 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.


  •  

    Tags for this Thread

    Posting Permissions

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