473,708 Members | 2,436 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

programmatic firing of keydown event

Can anyone help me with how to programmaticall y fire a keydown event in
Firefox?

Sep 8 '06 #1
9 16702

runner7 wrote:
Can anyone help me with how to programmaticall y fire a keydown event in
Firefox?

I don't think it's possible to cause a keyDown event for security
reasons, or if you can I don't think you can set the keyCode. I might
be wrong though, so don't take this as a final answer.

Sep 8 '06 #2
As best as I can tell from "developer.mozi lla" and "w3c" sites, the
following code should work in Firefox, but I cannot get it to work:

function keyFire(e) {
var e1=document.cre ateEvent("Keybo ardEvent");
e1.initKeyboard Event("keydown" ,true,true,wind ow,"U+0009",
DOM_KEY_LOCATIO N_STANDARD,null );
document.forms[0].elements[0].dispatchEvent( e1);
e.preventDefaul t();
e.stopPropagati on();
}

"U+0009" happens to be for the tab key. Has anyone got a clue?

Sep 9 '06 #3
runner7 wrote:
As best as I can tell from "developer.mozi lla" and "w3c" sites, the
following code should work in Firefox, but I cannot get it to work:

function keyFire(e) {
var e1=document.cre ateEvent("Keybo ardEvent");
e1.initKeyboard Event("keydown" ,true,true,wind ow,"U+0009",
DOM_KEY_LOCATIO N_STANDARD,null );
document.forms[0].elements[0].dispatchEvent( e1);
e.preventDefaul t();
e.stopPropagati on();
}

"U+0009" happens to be for the tab key. Has anyone got a clue?
<body onKeyDown="
if ( window.event ) {
alert( window.event.ke yCode );
}
else
if ( event ) {
alert( event.which );
}
">

--
Bart

Sep 9 '06 #4
I am working on this

http://www.cookdotcom.com/EventTest.html

note FX uses keypress

Sep 9 '06 #5
Thanks guys for your answers, but I think I need to clarify what I am
trying to do. I am not trying to detect a keystroke or what key was
pressed. I am trying to programmaticall y send the equivalent of a
keystroke to the Firefox browser.

In IE, the problem is trivial, because window.event.ke yCode can be set,
but in Firefox the event.which property appears to be read only.
Thanks to anyone for any further insight on this matter.

Sep 9 '06 #6
runner7 wrote:
Thanks guys for your answers, but I think I need to clarify what I am
trying to do. I am not trying to detect a keystroke or what key was
pressed. I am trying to programmaticall y send the equivalent of a
keystroke to the Firefox browser.
That equivalent is actually just the decimal code point of the
character.

alert('W'.charC odeAt(0));

should show you '87', the same cp as when pressing 'W' in my previous
snippet. There are some things you should be aware of though (e.g.
upper/lower case), but you can send/retrieve such values wherever you
want.
In IE, the problem is trivial, because window.event.ke yCode can be set,
but in Firefox the event.which property appears to be read only.
I don't think this is correct. AFAIK, keyCode should be read-only
too.

Your problem is more fundamental. You want to make javascript think the
user pressed a key, while he actually didn't press it. It's spoofing by
definition - browsers are right to complain about this kind of
trickery. My gut feeling is that you should give up your plan and
rethink your coding strategy.

And honestly I can't think of any good reason why you would want to
fake a keystroke, besides theoretical or hacking motives.

--
Bart

Sep 10 '06 #7
Bart Van der Donck wrote:
In IE, the problem is trivial, because window.event.ke yCode can be set,
but in Firefox the event.which property appears to be read only.

I don't think this is correct. AFAIK, keyCode should be read-only
too.
(AFAIK==as far as I know?)

The following code works in IE to replace an "enter" keystroke with a
"tab" keystroke in the first text box:

<html>
<head><title> </title>
<script type="text/javascript">
function replace(e) {
if (e.keyCode==13) e.keyCode=9
}
</script>
</head>
<body>
<form>
<input onkeydown="repl ace(event)" /><br />
<input />
</form>
</body>
</html>

If you replace the "keyCode" property with the "which" property for
Firefox, the code does not work in Firefox.

Bart Van der Donck wrote:
Your problem is more fundamental. You want to make javascript think the
user pressed a key, while he actually didn't press it. It's spoofing by
definition - browsers are right to complain about this kind of
trickery. My gut feeling is that you should give up your plan and
rethink your coding strategy.

And honestly I can't think of any good reason why you would want to
fake a keystroke, besides theoretical or hacking motives.
The purpose is for a data entry application, such as when using the
numeric keypad to enter data. Typically, the "enter" key will work
like a "tab" key until the last field on the form. Using
String.fromChar Code(9) to append to the fields's value only appends a
"tab" character just like in a word processing application, but does
not cause the cursor to move to the next field. Any ideas on how to
make "enter" work like "tab" in Firefox?

Sep 10 '06 #8
runner7 wrote:
Bart Van der Donck wrote:
<snip>
>And honestly I can't think of any good reason why you
would want to fake a keystroke, besides theoretical or
hacking motives.

The purpose is for a data entry application, such as when
using the numeric keypad to enter data. Typically, the
"enter" key will work like a "tab" key until the last
field on the form.
Is that typical of web browsers? That has not been my experience.
Using String.fromChar Code(9) to append to the fields's
value only appends a "tab" character just like in a
word processing application, but does not cause the cursor
to move to the next field. Any ideas on how to
make "enter" work like "tab" in Firefox?
While fixating on the insoluble key event problem you are falling to
notice that what you want to do is focus the next field in the tabbing
sequence in response to a key event. That can certainly be done (on the
limited number of browsers you seem interested in) by identifying the
significant key event, cancelling it, identifying the next filed and
explicitly calling its - focus - method. It may not be as simple as
changing the key event, or faking them, but when neither of those will
do it is a viable alternative.

Richard.
Sep 10 '06 #9
runner7 wrote:
The purpose is for a data entry application, such as when
using the numeric keypad to enter data. Typically, the
"enter" key will work like a "tab" key until the last
field on the form.

Richard Cornford wrote:
Is that typical of web browsers? That has not been my experience.
I meant desktop data-entry applications. This one happens to have a
browser interface.

Richard Cornford wrote:
That can certainly be done (on the
limited number of browsers you seem interested in) by identifying the
significant key event, cancelling it, identifying the next filed and
explicitly calling its - focus - method.
Thank you. That is the solution. I wish I could say I had thought of
it. I am still wondering about the DOM 3 "KeyboardEv ent" though, which
developer.mozil la.org says is supposed to be supported by Firefox's
Gecko layout engine.

Sep 11 '06 #10

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

Similar topics

3
2928
by: Fabrício de Novaes Kucinskis | last post by:
Hi all, I have a combobox control, in wich I put an autocomplete code. This code is in the TextChanged event, but when this event fires, the Text property isn't updated yet. For example, if the Text property is "Foo" and I press the letter "d", the Text property when the TextChanged event runs is "Foo". Only after the TextChanged event be processed, the KeyDown, KeyUp and KeyPress events fires and update the Text property.
0
1448
by: CoderOfBugs | last post by:
I am developing an IE Toolbar that has a combo box. The combox box doesn't fire the KeyDown event when the {Backspace} key is pressed. Very strange. The event fires for {Delete}, {Space}, {CTRL}, etc. Does it have something to do with {Backspace} being the hot key for going to the previous page? Code: this.cboSearch.KeyDown += new KeyEventHandler(this.cboSearch_KeyDown); private void cboSearch_KeyDown (object sender, KeyEventArgs e)
1
1746
by: Quimbly | last post by:
My KeyDown / KeyUp / KeyPress events are not firing; I can't figure out what I'm doing wrong. The code I'm using is basically straight from the manual. "this" refers to my Windows.Forms.Form object. this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.MainForm_KeyDown); this.KeyPress += new
3
2024
by: DotNetJunkies User | last post by:
Hi, I have a checkbox control programmatically created for each row in the datagrid but CheckedChanged event not firing when state of the checkbox changes. I use the following code to instantiate ItemTemplate with checkbox contructor: public class TemplateObject : ITemplate { public void InstantiateIn(Control objContainer) { CheckBox objCheckBox = new CheckBox(); objCheckBox.ID = "Item";
4
3335
by: Dan | last post by:
Hi, I have a timer on a form (System.Windows.Forms.Timer - Framework 1.1) that is set to 60 seconds as sort an of inactivity monitor. If 60 seconds have elapsed without any user activity I want the form to close. I stop/start the timer at button clicks and keydown events etc of the various controls on the form. Users have reported the form closing in the middle of them typing or immediately after opening the form (but only rarely)....
2
4169
by: Phil Galey | last post by:
I have a Panel control docked on all sides on a form and the panel control contains a PictureBox. I'm using the KeyDown event of the form to respond to the and keys for resizing the image and the PageUp, PageDn, Home, End, and arrow keys for scrolling the Panel control. Resizing the image using the and keys works fine, deriving a resized thumbnail from the image and reassigning it to the Image property of the PictureBox. However I'm...
5
1837
by: Datagridtextboxcolumn not firing event | last post by:
"PLEASE HELP, I NEED HELP N O W !!!" AddHandler dgtxtboxcolumn.textbox.KeyDown, AddressOf TextBoxKeyPress Private Sub TextBoxKeyPress(ByVal sender as Object, ByVal e as KeyEventArgs) End Sub Not Firing the Enter Key event except Other Keys.
2
2158
by: Lenster | last post by:
Environment --------------- Visual Studio.NET 2003 Version 7.1.3088 ..NET Framework 1.1 Version 1.1.4322 SP1 XP Professional 5.1.2600 SP2 Build 2600 Problem Description ----------------------- I have an mdi parent form.
4
13539
by: velasquez.m | last post by:
i'm working with visual studio 2005 and have a solution with 16 projects in it. this specific application has a base form with other forms added to it at any area of the application that the user navigates to through the nav panel anyways, on one of the forms i'm attempting to have a configuration setting set to a hotkey press for the user to be able to reset a feature with a hotkey combination
0
8787
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
8697
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
9289
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
9158
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7921
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
6615
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
5939
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
4454
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2508
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.