ctrlKey property in IE

H

Hiroshi Ochi

I keep getting 'member not found' error if I try to do the below
command:
//
window.event.ctrlKey = false; // assigning value to property
//

Is it not allowed to change the window.event.ctrlKey value? But
according to the reference in
http://msdn.microsoft.com/library/d...author/dhtml/reference/properties/ctrlkey.asp
, the ctrlKey property is read/write in IE 5 and later?

Would anybody give me a clue? Is there any way that I can 'cancel' the
pressed ctrlKey?

Thanks!
Hiroshi
 
M

Michael Winter

I keep getting 'member not found' error if I try to do the below
command:
//
window.event.ctrlKey = false; // assigning value to property
//

Is it not allowed to change the window.event.ctrlKey value?

I would argue, no, you shouldn't be able to...
But according to the reference in , the ctrlKey property is
read/write in IE 5 and later?


....but as you say, they do indicate it's writable. However, Microsoft's
documentation says many things that aren't true, so it's not that
surprising.

I believe that the read/write status is only true under one condition:
when the user creates an event object using Microsoft's
document.createEventObject() method. The event object returned uses
default values, so the user must modify the information as appropriate. It
can then be fired with Microsoft's obj.fireEvent() method[1].

With pre-existing events - that is, those generated by the browser - all
properties are read-only. They are, after all, supposed to represent the
state of the system when the event was fired, so allowing an author to
change that at a whim would corrupt the data.
Would anybody give me a clue? Is there any way that I can 'cancel' the
pressed ctrlKey?

Not directly. You'll have to add some form of logic to ignore the key. If
this is used to change, say CTRL+K to K, you can return a value that
represents the character code of the key, effectively over-riding what the
user did. However, I think this is only supported by IE, and I haven't
tested if it will be of any use - you didn't give enough information.

Hope that helps,
Mike


[1] I keep referring to "Microsoft's" methods, because they do not conform
to the W3C DOM 2 Event specification.[/QUOTE]
 
D

DU

Hiroshi said:
I keep getting 'member not found' error if I try to do the below
command:
//
window.event.ctrlKey = false; // assigning value to property
//

Is it not allowed to change the window.event.ctrlKey value? But
according to the reference in
http://msdn.microsoft.com/library/d...author/dhtml/reference/properties/ctrlkey.asp
, the ctrlKey property is read/write in IE 5 and later?

Would anybody give me a clue? Is there any way that I can 'cancel' the
pressed ctrlKey?

Thanks!
Hiroshi

Let me get this straight. The visitor of your webpage presses the <Ctrl>
key and your javascript function will neutralize it in MSIE... or this
is what you want to happen in MSIE. Did I get this right?
Can you explain your webpage context, situation, requirements? What's
its url?

DU
 
H

Hiroshi Ochi

Thanks Michael and DU,

To enhanced usability, I would like to set the TAB behaviour if user
presses 'CTRL + Right Arrow' key and to set the SHIFT + TAB behaviour if
user presses 'CTRL + Left Arrow' key.

I especially need this function in HTML Tables. For the time being, I
can only concentrate for compatibility with MSIE ver.6.0 and above.

Below is the code that I want to implement.

//
if (!window.event.ctrlKey) return true;
try {
switch(window.event.keyCode){
case 37: // left
window.event.keyCode = 9;
window.event.ctrlKey = false; // <-- this is the problem
break;
case 39: // right
window.event.keyCode = 9;
window.event.ctrlKey = false; // <-- this is the problem
window.event.shiftKey = true; // <-- this is also problem..
break;
}
return true;
}
catch(exception )
{
return false;
}

Thanks in advanced!

rgs,
hiroshi
 
M

Michael Winter

To enhanced usability, I would like to set the TAB behaviour if user
presses 'CTRL + Right Arrow' key and to set the SHIFT + TAB behaviour if
user presses 'CTRL + Left Arrow' key.

I especially need this function in HTML Tables. For the time being, I
can only concentrate for compatibility with MSIE ver.6.0 and above.

You could investigate something I touched on in my post: firing your own
event. Your code would basically be structured:

// e - is the current event (window.event)

var newEvent = document.createEventObject( e );

newEvent.keyCode = 9;
newEvent.ctrlKey = false; // this won't error :)

e.srcElement.fireEvent( 'eventName', newEvent );

'eventName' is probably 'onkeypress'. You should also be able to replace
'e.srcElement' with 'this'.

You might run into problems if you do decide to support browsers other
than IE. At present, the W3C have delayed, perhaps permanently, the
publication of the DOM 3 Events specification as a Recommendation. This
document was set to introduce keyboard events, which are currently missing
from the DOM 2 Events spec. Mozilla has implemented keyboard events from
DOM 3 Events, but they used an early version which has since changed
method and interface names.

Good luck,
Mike
 
D

DU

Hiroshi said:
Thanks Michael and DU,

To enhanced usability,

People with disabilities or using special browsers (text-to-speech
synthetizing browser or braille browser or just MSIE with accessibility
features triggered with keyboard combos) might have problem using a page
with such reformating.

I would like to set the TAB behaviour if user
presses 'CTRL + Right Arrow' key and to set the SHIFT + TAB behaviour if
user presses 'CTRL + Left Arrow' key.

If I understood you correctly, you want the user's browser to react like
if the user typed in <tab> if the user typed <Ctrl>+<right arrow> and
the user's browser to react like <Shift>+<tab> if the user typed
<Ctrl>+<left arrow>. You're asking to reformat 2 keyboard combination
shortcuts to act like 2 other ones, to duplicate 2 other ones.

Now, this could be doable in MSIE 6 for windows and in NS 7.x and
Mozilla-based browsers. It could be doable also with Opera 7.x; I'm less
sure about this one though. But I would really want to *first*
understand why you would want to reformat the key stroke combinations
(and duplicate the already working and well established functionality of
another key combo) and I sure would want to see first the page which
supposedly need such reformating and javascript coding.

DU
 
H

Hiroshi Ochi

Thanks again Mike!

I tried your solution, and managed to change the value of ctrlKey
property. But now I realize that eventhough I can change the value of
ctrlKey and keyCode, it won't behave like tab key. Do you happen to know
why? Below is my sample code.

<script>
document.onkeydown=onkeydownArrowsHandler;

function onkeydownArrowsHandler(e) {
e = e||window.event;
var o = (e.srcElement||e.target);
//omit the following
if(!o) return true;
if(o.tagName != "TEXTAREA" && o.tagName != "INPUT" && o.tagName !=
"SELECT") return true;
// alert(e.keyCode);
// alert(e.ctrlKey);
// alert(e.shiftKey);
if(!e.ctrlKey) return true;
if(!(e.keyCode == 37 || e.keyCode == 39 )) return true;

//create event object
var newEvent = document.createEventObject(e);

newEvent.keyCode = 9;
newEvent.ctrlKey = false;

if(e.keyCode == 37) //if arrow left, shift + tab
newEvent.shiftKey = true;

o.fireEvent( 'onkeydown', newEvent );

return true;
}
</script>



Thanks,
hiroshi
 
H

Hiroshi Ochi

Hello DU,

Thanks for your comment. Yes, I wish the behaviour something like
phpmyadmin tables.

To get the picture of what I am talking about, please access
http://www.phpmyadmin.net/phpMyAdmin/ and click PMA_bookmark table in
the left frame. Click 'Check All' to mark all checkbox, and in 'With
Selected:' click the 'Change' icon.

You will be directed to tbl_properties_structure.php page, and in the
table shown, try to press <ctrl> and arrow keys to move
up,down,right,left. I am expecting the behaviour to be like this.

In phpmyadmin, as you might know, their trick is to set the whole field
with id, and focus the cursor using the unique id (x and y axis) for
each field. In our application (we are creating a web-based sales-order
management) we can not use this idea because user can dynamically add or
delete a certain row, and according to user authorization, we
dynamically hide a certain column by setting the style to
'display:none'. And in some cases, we have nested tables or multiple
input in one <td> tag.

I did try to moving around by referencing the nextSibling or
previousSibling, but there are too many logic must be implemented
inside. Especially for the moving to left and right side. For up and
down, we can settle more or less 80% of the cases, but for left and
right, still far from complete. It is therefore the safest way to
'mimic' the behaviour of <tab> key for <ctrl>+<right arrow> and
<shift>+<tab> key for <ctrl>+<left arrow> key.

I understand your argument of why bother changing the behaviour of well
known key, but I also believe that if user can use <ctrl> key and arrow,
the behaviour is more consistent and more user friendly. What do you
think?

rgs,
hiroshi
 
M

Michael Winter

I tried your solution, and managed to change the value of ctrlKey
property. But now I realize that eventhough I can change the value of
ctrlKey and keyCode, it won't behave like tab key. Do you happen to know
why? Below is my sample code.

[snip]

I tried various methods myself, and couldn't get IE to act on the
simulated events. It's strange because I was under the impression that the
whole point of faking an event was to get the UI to act as though the user
caused said event.

An alternative that I've been looking at involves the tree navigation,
using nextSibling and previousSibling, which you wanted to avoid. However,
you should be able to avoid the problems you encountered by making good
use of the tabindex attribute (tabIndex property) to ensure that elements
are traversed in the correct sequence, irrespective of the complexity of
underlying HTML. The idea involved walking the tree in its entirety
(unless only a particular area of the document is of interest), adding
controls to a multi-dimensional array, the initial elements being a
tabIndex. The current tabindex level and active control are recorded, to
that when the user navigates using your system, the next control can be
found. Monitoring changes made through other methods (tabbing and
clicking) complicates things a little, but it's still feasible. Certainly
not simple, though.

Mike
 
J

Jim Ley

I tried your solution, and managed to change the value of ctrlKey
property. But now I realize that eventhough I can change the value of
ctrlKey and keyCode, it won't behave like tab key. Do you happen to know
why? Below is my sample code.

[snip]

I tried various methods myself, and couldn't get IE to act on the
simulated events. It's strange because I was under the impression that the
whole point of faking an event was to get the UI to act as though the user
caused said event.

Remember simulated events can't trigger things in the application,
only in the page, tabkeys are being treated as application level
things presumably.

Jim.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top