473,396 Members | 1,915 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Simulate keypress

Hello,

As the subject says it, is there a way to simulate a special keypress in JS
? In my case, on loading an HTML page, i'd like to set the cursor at the end
of the input text of an <input type=text ...> object.
focus() sets the cusor at the beginning

Any idea ?

Christian.
Jul 20 '05 #1
2 11236
First of all sorry about my poor English, i´ll try to do the best.

Well, i´m having a similar problem because i wan´t to select certaint
characters from a text box. Maybe this routine i´ve just created , solves
your problem.

This routine, selects in a textbox characters from StartIndex to EndIndex.
If you gives to the routine both parameters with textbox content lenght, i
think it puts the cursor in the desired position.

function selectFromTo (textInput, charStartIndex, charEndIndex) {

/* We´ll calculate the appropiate value to the different moves. */
strTextInputValue = "" + textInput.value;

charStartIndex = (parseInt(charStartIndex) - 1);

numEndIndex = parseInt(charEndIndex);
numEndIndex -= strTextInputValue.length;
charEndIndex = numEndIndex.toString();

if (textInput.createTextRange) {
var range = textInput.createTextRange();
range.moveStart('character', charStartIndex );
range.moveEnd('character', charEndIndex);
range.select();
}
}

Raul Carrillo a.k.a Metsuke
"Christian" <cg********@yahoo.com> escribió en el mensaje
news:bo**********@reader1.imaginet.fr...
Hello,

As the subject says it, is there a way to simulate a special keypress in JS ? In my case, on loading an HTML page, i'd like to set the cursor at the end of the input text of an <input type=text ...> object.
focus() sets the cusor at the beginning

Any idea ?

Christian.

Jul 20 '05 #2
"Raul Carrillo a.k.a. Metsuke <(^_^)>" <rc*******@coleccionalo.com> wrote in
message news:9i**********************@telenews.teleline.es ...
First of all sorry about my poor English, i´ll try to do the best.

Well, i´m having a similar problem because i wan´t to select certaint
characters from a text box. Maybe this routine i´ve just created , solves
your problem.

This routine, selects in a textbox characters from StartIndex to EndIndex.
If you gives to the routine both parameters with textbox content lenght, i
think it puts the cursor in the desired position.

function selectFromTo (textInput, charStartIndex, charEndIndex) {

/* We´ll calculate the appropiate value to the different moves. */
strTextInputValue = "" + textInput.value;

charStartIndex = (parseInt(charStartIndex) - 1);

numEndIndex = parseInt(charEndIndex);
numEndIndex -= strTextInputValue.length;
charEndIndex = numEndIndex.toString();

if (textInput.createTextRange) {
var range = textInput.createTextRange();
range.moveStart('character', charStartIndex );
range.moveEnd('character', charEndIndex);
range.select();
}
}

Raul Carrillo a.k.a Metsuke
"Christian" <cg********@yahoo.com> escribió en el mensaje
news:bo**********@reader1.imaginet.fr...
Hello,

As the subject says it, is there a way to simulate a special keypress in

JS
? In my case, on loading an HTML page, i'd like to set the cursor at the

end
of the input text of an <input type=text ...> object.
focus() sets the cusor at the beginning

Any idea ?

Christian.


Thanks for the reply. I eventually found a similar answer on google, using
createTextRange. That will solve my problem, though i doubt this will work
on every browser.

For a more general matter, is there a way to simulate a keypress ?
Jul 20 '05 #3

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

Similar topics

3
by: Darryn Ross | last post by:
Hi, I am trying to catch the KeyPress event on my datagrid but it isn't working... i have also tried registering the handler with the event like this... dgGLBatch.KeyPress += new...
2
by: rege | last post by:
Hi, I have two applications developed in C# say A and B. When user presses key "L" with applicaition A active , it causes a keypress event which causes application B to run. Application A then...
1
by: Rene | last post by:
Hi, I am running is some problems with the KeyPreview and KeyPress events. The KeyPress event is only triggered when there this an focusable control on the form. When all controls are disabled...
3
by: Dan | last post by:
Consider a simple form with a textbox and button. Is there a way to handle the common situation where a user presses "return" rather than clicks on the button. I can already detect the...
4
by: Tom | last post by:
I have a VB.NET user control that I wrote - this control has three or four other controls on it (textbox, combobox, datetime picker, etc). Now, whenever the control is on a form and the user enters...
15
by: Adam J. Schaff | last post by:
I have noticed that if a user closes a form via pressing return (either while the OK button has focus or if AcceptButton is set to OK for the form) then the "ENTER" keypress event fires ON THE...
3
by: Fia | last post by:
Hi In Visual Basic 6 I could call keypress with an integer of a choosen key. For example I could call a textbox's keypress with the number 13 (for the enter key). But now in Visual Basic .Net I...
5
by: Henry Jones | last post by:
I am new to C# and wanted to capture the KeyPress for a textbox. I created some code as follows: private void textBox3_KeyPress(object sender, System.EventArgs e) { this.textBox2.Text =...
3
by: =?Utf-8?B?cm9kY2hhcg==?= | last post by:
hey all, in my code-behind windows project (vs2005) i was wondering if you can simulate the pressing of key combinations (i.e. Alt + S)? thanks, rodchar
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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,...

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.