473,734 Members | 2,300 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Shift-click, control-click

Dear All,

I am working on a piece of Javascript code that needs to detect a
mouse-click, shift-click and control-click. The code is not my own -
it is a part of a much larger suite of routines. The code as it stands
does not work in Firefox, and I suspect that the task of feature
detection (which currently depends on browser detection) can be
carried out better. The result should be that opcode is set to 0,1 or
2 dor click, control-click and shift-click. I post the entire function
below.

Thanks

Paul

function click(e)
{
if(!e)
e=window.event;
var mX=null;
var mY=null;
var opcode = 0; //0=zoom in, 1=zoom to extent (ctrl), 2=pan
(shift)
var op=navigator.us erAgent.toLower Case().indexOf( 'opera')!=-1;
var ie = document.all;
if (ie || op)
{
mX = e.offsetX;
mY = e.offsetY;
if (e.ctrlKey)
opcode = 1;
if (e.shiftKey)
{
opcode += 2;
}
}
else
{
mX = e.layerX;
mY = e.layerY;
if (e.modifiers & Event.CONTROL_M ASK)
{
opcode = 1;
}
if (e.modifiers & Event.SHIFT_MAS K)
{
opcode += 2;
}
}
if (opcode == 0)
{
this.zoom(mX,mY );
}
else if (opcode == 1)
{
this.zoomOut(mX ,mY);
}
else if (opcode == 2)
{
this.pan(mX,mY) ;
}
else if (opcode == 3)
{
this.queryUI(mX ,mY);
}
}

Jul 23 '05 #1
7 15502
Paul Cooper wrote:
Dear All,

I am working on a piece of Javascript code that needs to detect a
mouse-click, shift-click and control-click. The code is not my own -
it is a part of a much larger suite of routines. The code as it stands does not work in Firefox, and I suspect that the task of feature
detection (which currently depends on browser detection) can be
carried out better. The result should be that opcode is set to 0,1 or
2 dor click, control-click and shift-click. I post the entire function below.

Thanks

Paul

function click(e)
{
if(!e)
e=window.event;
var mX=null;
var mY=null;
var opcode = 0; //0=zoom in, 1=zoom to extent (ctrl), 2=pan
(shift)
var op=navigator.us erAgent.toLower Case().indexOf( 'opera')!=-1;
var ie = document.all;
if (ie || op)
{
mX = e.offsetX;
mY = e.offsetY;
if (e.ctrlKey)
opcode = 1;
if (e.shiftKey)
{
opcode += 2;
}
}
else
{
mX = e.layerX;
mY = e.layerY;
if (e.modifiers & Event.CONTROL_M ASK)
{
opcode = 1;
}
if (e.modifiers & Event.SHIFT_MAS K)
{
opcode += 2;
}
}
if (opcode == 0)
{
this.zoom(mX,mY );
}
else if (opcode == 1)
{
this.zoomOut(mX ,mY);
}
else if (opcode == 2)
{
this.pan(mX,mY) ;
}
else if (opcode == 3)
{
this.queryUI(mX ,mY);
}
}


Hi Paul.

Mouse/kb/event routines are always iffy cross-platform; test this if
interested.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>formatte r</title>
<style type="text/css">

div#thingy {
width: 500px;
height: 360px;
margin: 80px auto;
border: thin black solid;
background: skyblue;
cursor: crosshair;
}
div#readout {
width: 150px;
height: 13px;
font: 11px arial;
text-align: center;
padding-bottom: 1px;
margin: 20px;
border: 1px black solid;
background: silver;
}

</style>
<script type="text/javascript">

function getOffIt(e, obj)
{
var o = { offX: obj.offsetLeft, offY: obj.offsetTop };
while (obj && (obj = obj.offsetParen t))
{
o.offX += obj.offsetLeft;
o.offY += obj.offsetTop;
}
if ('undefined' != typeof e.pageX)
{
o.offX = e.pageX - o.offX;
o.offY = e.pageY - o.offY;
}
else if ('undefined' != typeof e.clientX)
{
o.offX = e.clientX - o.offX;
o.offY = e.clientY - o.offY;
}
else return null;
return o;
}

function click(e)
{
var tgt, o, opcode = 0;
if ((e = e || window.event)
&& (tgt = e.srcElement || e.target)
&& (o = getOffIt(e, tgt)))
{
var mX = o.offX;
var mY = o.offY;
opcode += (e.ctrlKey && 1) + (e.shiftKey && 2);
switch (opcode)
{
case 0 :
stat('zoom', mX, mY);
//this.zoom(mX, mY);
break;
case 1 :
stat('zoomOut', mX, mY);
//this.zoomOut(mX , mY);
break;
case 2 :
stat('pan', mX, mY);
//this.pan(mX, mY);
break;
case 3 :
stat('queryUI', mX, mY);
//this.queryUI(mX , mY);
break;
default:
break;
}
}
if (e.cancelBubble )
{
e.cancelBubble = true;
e.returnValue = false;
}
else if (e.preventDefau lt)
e.preventDefaul t();
}

function stat(op, mX, mY)
{
var el = document.getEle mentById('reado ut');
el.innerHTML = op + ' | mX: ' + mX + ' | mY: ' + mY;
}

window.onload = function()
{
var el;
if (document.getEl ementById
&& (el = document.getEle mentById('thing y')))
{
if (el.attachEvent )
el.attachEvent( 'onclick', click);
else if (el.addEventLis tener)
el.addEventList ener('click', click, true);
else el.onclick = click;
}
}

</script>
</head>
<body>
<div id="readout">o p | x | y</div>
<div id="thingy"></div>
</body>
</html>

Jul 23 '05 #2
Paul Cooper <pa*********@No basspam.ac.uk> wrote in message
news:3d******** *************** *********@4ax.c om...
Dear All,

I am working on a piece of Javascript code that needs to detect a
mouse-click, shift-click and control-click. The code is not my own -
it is a part of a much larger suite of routines. The code as it stands
does not work in Firefox, and I suspect that the task of feature
detection (which currently depends on browser detection) can be
carried out better. The result should be that opcode is set to 0,1 or
2 dor click, control-click and shift-click.

<snip>

The following code reports the modifier key status on NN4, Mozilla/Firefox,
Opera and I.E.
I could make it work universally for shift and ctrl only by using the
mousedown handler.
I'm not sure if the reported co-ordinates will be correct.
<HTML>
<HEAD>
</HEAD>
<BODY >

Mouse-down on page with modifier keys and read status line/title bar.

<SCRIPT>

function mClick(e)
{

if(typeof(e)==' undefined')
e=window.event| |window.Event;

var mX=null;
var mY=null;
var opcode = 0; file://0=zoom in, 1=zoom to extent (ctrl), 2=pan(shift)

if ( typeof(e.offset X)!='undefined' )
{
mX = e.offsetX;
mY = e.offsetY;
}
else
if(typeof(e.lay erX)!='undefine d')
{
mX = e.layerX;
mY = e.layerY;
}

if (typeof(e.ctrlK ey)!='undefined ')
{
if (e.ctrlKey)
opcode |= 1;
if (e.shiftKey)
opcode |= 2;
}
else
{
if (e.modifiers & Event.CONTROL_M ASK)
opcode |=1;

if (e.modifiers & Event.SHIFT_MAS K)
opcode |= 2;
}

window.status=" opcode="+opcode +" window.event="+ window.event+
" window.Event="+ window.Event;
document.title= "opcode="+opcod e+" window.event="+ window.event+
" window.Event="+ window.Event;
}

if(window.Event )
document.captur eEvents(Event.M OUSEDOWN)

document.onmous edown=mClick;

</SCRIPT>
</BODY>
</HTML>

--
Stephen Chalmers


Jul 23 '05 #3
On Mon, 25 Apr 2005 15:14:22 +0100, Paul Cooper wrote:
Dear All,

I am working on a piece of Javascript code that needs to detect a
mouse-click, shift-click and control-click. The code is not my own - it
is a part of a much larger suite of routines. The code as it stands does
not work in Firefox, and I suspect that the task of feature detection
(which currently depends on browser detection) can be carried out
better.


I'm not surprised it doesn't work in Firefox. At the very least ctrl-click
(open link in new tab) and shift-click (open link in new window) are used
for functions in Firefox and won't get sent to the page itself no matter
what your code does.

--
Life is short, but wide. -KV

Jul 23 '05 #4
On Mon, 25 Apr 2005 15:44:20 -0500, Ivan Marsh <an*****@you.no w>
wrote:
On Mon, 25 Apr 2005 15:14:22 +0100, Paul Cooper wrote:
Dear All,

I am working on a piece of Javascript code that needs to detect a
mouse-click, shift-click and control-click. The code is not my own - it
is a part of a much larger suite of routines. The code as it stands does
not work in Firefox, and I suspect that the task of feature detection
(which currently depends on browser detection) can be carried out
better.


I'm not surprised it doesn't work in Firefox. At the very least ctrl-click
(open link in new tab) and shift-click (open link in new window) are used
for functions in Firefox and won't get sent to the page itself no matter
what your code does.

Well, I've got it working, so I am afraid you are wrong! I have done a
fairly simple modification of the code to make it work, but if you
think of it a mouse-click has an event attached to it. The event has
properties (in all browsers) that allow you to determine the state of
the control, shift and alt keys. My problem was simply that not all
browsers handle the event in the same way. I won't post my current
code as it still contains browser dependent code, but I'll post the
final version.

Paul
Jul 23 '05 #5
On Mon, 25 Apr 2005 21:36:26 +0100, "Stephen Chalmers"
<ig******@lycos .co.uk> wrote:
Paul Cooper <pa*********@No basspam.ac.uk> wrote in message
news:3d******* *************** **********@4ax. com...
Dear All,

I am working on a piece of Javascript code that needs to detect a
mouse-click, shift-click and control-click. The code is not my own -
it is a part of a much larger suite of routines. The code as it stands
does not work in Firefox, and I suspect that the task of feature
detection (which currently depends on browser detection) can be
carried out better. The result should be that opcode is set to 0,1 or
2 dor click, control-click and shift-click.

<snip>

The following code reports the modifier key status on NN4, Mozilla/Firefox,
Opera and I.E.
I could make it work universally for shift and ctrl only by using the
mousedown handler.
I'm not sure if the reported co-ordinates will be correct.
<HTML>
<HEAD>
</HEAD>
<BODY >

Mouse-down on page with modifier keys and read status line/title bar.

<SCRIPT>

function mClick(e)
{

if(typeof(e)==' undefined')
e=window.event| |window.Event;

var mX=null;
var mY=null;
var opcode = 0; file://0=zoom in, 1=zoom to extent (ctrl), 2=pan(shift)

if ( typeof(e.offset X)!='undefined' )
{
mX = e.offsetX;
mY = e.offsetY;
}
else
if(typeof(e.lay erX)!='undefine d')
{
mX = e.layerX;
mY = e.layerY;
}

if (typeof(e.ctrlK ey)!='undefined ')
{
if (e.ctrlKey)
opcode |= 1;
if (e.shiftKey)
opcode |= 2;
}
else
{
if (e.modifiers & Event.CONTROL_M ASK)
opcode |=1;

if (e.modifiers & Event.SHIFT_MAS K)
opcode |= 2;
}

window.status=" opcode="+opcode +" window.event="+ window.event+
" window.Event="+ window.Event;
document.title= "opcode="+opcod e+" window.event="+ window.event+
" window.Event="+ window.Event;
}

if(window.Even t)
document.captur eEvents(Event.M OUSEDOWN)

document.onmou sedown=mClick;

</SCRIPT>
</BODY>
</HTML>

Thanks - this looks like just the thing I want.

Paul
Jul 23 '05 #6
Paul Cooper <pa*********@No basspam.ac.uk> wrote in message
news:sn******** *************** *********@4ax.c om...
On Mon, 25 Apr 2005 21:36:26 +0100, "Stephen Chalmers"
<ig******@lycos .co.uk> wrote:
Thanks - this looks like just the thing I want.

Paul


I'm glad it's of some use.
--
Stephen Chalmers http://makeashorterlink.com/?H3E82245A


Jul 23 '05 #7
On Tue, 26 Apr 2005 15:56:13 +0100, "Stephen Chalmers"
<ig******@lycos .co.uk> wrote:
Paul Cooper <pa*********@No basspam.ac.uk> wrote in message
news:sn******* *************** **********@4ax. com...
On Mon, 25 Apr 2005 21:36:26 +0100, "Stephen Chalmers"
<ig******@lycos .co.uk> wrote:
Thanks - this looks like just the thing I want.

Paul


I'm glad it's of some use.


I have just modified my code in line with your suggestion and so far
it looks good - your concerns about the X,Y positions seem to be
unfounded.

Paul
Jul 23 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
2631
by: Glen Able | last post by:
Just to get my head straight on this... Firstly, am I right in thinking that right-shifting a signed integer has an undefined result (i.e. could be implemented as a logical or arithmetic shift)? If so, am I also right in assuming that if I right-shift a signed integer by an unsigned integer, the signed value will be promoted to unsigned and the shift will be performed as a logical right shift?
6
3736
by: PerryC | last post by:
I have search googles and there are hundreds of tips about AllowByPassKey... however, none works for me... well, perhaps I am too new to such high level functionality that it just does not make sense to me. So, anyone please help. I have no idea what is "CreateProperty" that Access help was trying to tell me to do. Can anyone please write me a step by step on how to accomplish this? (i.e. disable Shift on Startup.) I've seen many...
388
21757
by: maniac | last post by:
Hey guys, I'm new here, just a simple question. I'm learning to Program in C, and I was recommended a book called, "Mastering C Pointers", just asking if any of you have read it, and if it's worth the $25USD. I'm just looking for a book on Pointers, because from what I've read it's one of the toughest topics to understand. thanks in advanced.
43
26514
by: Mehta Shailendrakumar | last post by:
Hello, Can anyone suggest me operator to perform arithmetic shift in C? May it be for a perticular compiler. Thank you in advance. Regards, Shailendra
9
2615
by: deepak | last post by:
If i'm shifting an integer 'n' times where n > sizeof(int), It's giving the same value as that of n-sizeof(n). Why is it behaving like this. I studied that the bit 'll be replaced by '0'. Example; int i = 1; printf ("%d\n", i<<32);
2
4570
by: sytemper | last post by:
I write a keydown KeyDown event to catch if tab is press or tab+shift is press.And i already override the IsInputKey method. But noe i only can get the tab but no shift+tab. When shift+tab is press, i only get KeyCode=ShiftKey,and KeyData=65552, but nothing to show that tab is also pressed. What should i do now?
4
4289
by: sandhya | last post by:
Hello Folks, i hava a problem in coding of circular left shift of 25 bits in my program...how do i perform it, and how do i use unsigned in VB. My program (IDEA algorithm implementation in VB) requires unsigned bits...so how do i go thro this ,since VB does not support unsigned operations Post your suggestions!!!
3
4199
by: Amy Smith | last post by:
Hello there, I am having a small problem which been challenging me for few days and need help or advice. I am trying to calculate the day-shift for employees based on the time they started and finish working, I will only have 2 shifts 1 or 2 . Shift one changes based on the location however any thing else is shift 2. The problem I am having is when someone signed in after midnight; I need to report his time under shift 2 for the...
4
4190
by: Felix Kater | last post by:
Hi, when I use something like int Shift= 3; long Value= 1 << Shift; What is the data type of the const value '1' here? In other terms: What is the possible maximum of 'Shift' here?
3
11658
by: Pietro | last post by:
Hi all, First of all I'd like to thank you very very much ,as finally after many years of searching,I could find a code to disable/enable the shift key,but actually i cannot use the code as I'm very new to VBA,i tried to follow the instructions reported in the code,but i got no result,i still can use the shift key,can you explain in details how to use it correctly to enable/disable users from pressing shift key to view database windw?,the...
0
8948
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8780
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9456
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8190
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6742
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6035
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4816
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2733
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2182
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.