Javascript ctrl-click.

This is a discussion on Javascript ctrl-click. within the A Brief History of Cprogramming.com forums, part of the Community Boards category; I'm writing a minesweeper clone in javascript and since most browsers won't let you capture right clicks I need some ...

  1. #1
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,220

    Javascript ctrl-click.

    I'm writing a minesweeper clone in javascript and since most browsers won't let you capture right clicks I need some other way to flag a tile.

    One way I am considering is control click. I found this code on the web and it seems to work for Internet Explorer

    Code:
    ...
    	if (parseInt(navigator.appVersion) > 3) {
    		document.onmousedown = mouseDown;
     		if (navigator.appName == "Netscape") 
      			document.captureEvents(Event.MOUSEDOWN);
    	}
    ...
    
    /* this only works for IE at the moment */
    function mouseDown(e) {
    	if (parseInt(navigator.appVersion)>3) {
    		if (navigator.appName == "Netscape") {
    			var mString = (e.modifiers+32).toString(2).substring(3,6);
    			shiftPressed = (mString.charAt(0)=="1");
    			ctrlPressed = (mString.charAt(1)=="1");
    			altPressed = (mString.charAt(2)=="1");
      		} else {
       			shiftPressed= event.shiftKey;
       			altPressed = event.altKey;
       			ctrlPressed = event.ctrlKey;
      		}
     	}
    
     	return true;
    }
    but as you can see from my implementation at
    http://users.cs.cf.ac.uk/B.A.Collins
    it just doesn't work in any browser other than IE.

    Are there any other ways of capturing a control-click that aren't about 30 years old? Also I probably should've posted this in tech.
    Last edited by Brian; 01-06-2006 at 02:30 PM.

  2. #2
    Registered User
    Join Date
    Nov 2004
    Location
    India
    Posts
    515
    Works fine in Opera 8.5
    Code:
    >+++++++++[<++++++++>-]<.>+++++++[<++++>-]<+.+++++++..+++.[-]>++++++++[<++++>-] <.>+++++++++++[<++++++++>-]<-.--------.+++.------.--------.[-]>++++++++[<++++>- ]<+.[-]++++++++++.

  3. #3
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,220
    Quote Originally Posted by PING
    Works fine in Opera 8.5
    So it does. How do you stop that save image dialog coming up though?

  4. #4
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    That is very cool!

    The Mozilla implementation of the event object also supports the ctrlKey property so:
    Code:
    function mouseDown(e) 
    {
    	if (parseInt(navigator.appVersion) > 3)
    	{
    		var evt = (navigator.appName == "Netscape" ? e : window.event);
    
    		shiftPressed = evt.shiftKey;
    		altPressed   = evt.altKey;
    		ctrlPressed  = evt.ctrlKey;
     	}
    
     	return true;
    }

  5. #5
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,220
    Quote Originally Posted by anonytmouse
    That is very cool!

    The Mozilla implementation of the event object also supports the ctrlKey property so:
    Code:
    function mouseDown(e) 
    {
    	if (parseInt(navigator.appVersion) > 3)
    	{
    		var evt = (navigator.appName == "Netscape" ? e : window.event);
    
    		shiftPressed = evt.shiftKey;
    		altPressed   = evt.altKey;
    		ctrlPressed  = evt.ctrlKey;
     	}
    
     	return true;
    }
    kick ass that works, thanks.

  6. #6
    Cached User mako's Avatar
    Join Date
    Dec 2005
    Location
    Germany.Stuttgart
    Posts
    69

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. JavaScript book recommendations
    By neandrake in forum Tech Board
    Replies: 2
    Last Post: 04-05-2009, 01:27 PM
  2. Replies: 2
    Last Post: 03-10-2009, 09:36 PM
  3. Button click out of WM_COMMAND
    By Sober in forum Windows Programming
    Replies: 8
    Last Post: 05-03-2007, 10:07 PM
  4. Replies: 2
    Last Post: 03-23-2006, 07:08 AM
  5. newbie help
    By jcw96 in forum C++ Programming
    Replies: 10
    Last Post: 09-15-2002, 09:10 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21