473,387 Members | 1,515 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,387 software developers and data experts.

Virtual keyboard focus

hi all,

i have a html form with multiple text fields, accepting input from a
virtual onscreen keyboard.

so far i have document.forms[0].elements['usernameField'].value+=x;

The problem is that i dont want to hard code a text field name. it
should type in text wherever the focus is.

can somebody suggest a generic solution. thanks.

Dec 20 '05 #1
8 5251
sc*****@gmail.com said the following on 12/19/2005 11:50 PM:
hi all,

i have a html form with multiple text fields, accepting input from a
virtual onscreen keyboard.

so far i have document.forms[0].elements['usernameField'].value+=x;

The problem is that i dont want to hard code a text field name. it
should type in text wherever the focus is.
And it will do that. Unless you are trapping key events. Are you?
can somebody suggest a generic solution. thanks.


A URL to a sample page would be a great help.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Dec 20 '05 #2
Ivo
<sc*****@gmail.com> wrote
i have a html form with multiple text fields, accepting input from a
virtual onscreen keyboard. so far i have
document.forms[0].elements['usernameField'].value+=x;
Don't refer to forms by index number, general good practice has every form
named with a genuine id attribute.
The problem is that i dont want to hard code a text field name. it
should type in text wherever the focus is.


So you tell each textfield to call a function onfocus, which in that fashion
registers which element last received the focus, and accordingly maintains a
global variable to retrieve a reference to the element from later on when
you detect a keypress. By setting this variable to some element during its
declaration, you can supply a 'default' element to type in and remove the
need to check if it is set when the user first starts typing.

var focused = document.forms.f.elements.usernameField;
window.onload = function() {
var el = document.forms.f.elements; // form with id=f
el.usernameField.onfocus = el.yetanotherField.onfocus =
function() { focused = this; }
}

window.document.onkeydown = function () {
// you know to get x
focused.value += x;
}

hth
ivo
http://4umi.com/web/javascript/
Dec 20 '05 #3
Ivo said the following on 12/20/2005 1:39 AM:
<sc*****@gmail.com> wrote
i have a html form with multiple text fields, accepting input from a
virtual onscreen keyboard. so far i have
document.forms[0].elements['usernameField'].value+=x;

Don't refer to forms by index number, general good practice has every form
named with a genuine id attribute.


A better alternative is a genuine name attribute :)
The problem is that i dont want to hard code a text field name. it
should type in text wherever the focus is.

So you tell each textfield to call a function onfocus, which in that fashion
registers which element last received the focus, and accordingly maintains a
global variable to retrieve a reference to the element from later on when
you detect a keypress. By setting this variable to some element during its
declaration, you can supply a 'default' element to type in and remove the
need to check if it is set when the user first starts typing.

var focused = document.forms.f.elements.usernameField;


If using the forms collection, using a name is better because of better
support:

document.forms['elementNAMEnotID'].elements['elementNAMEnotID']

Just my two cents worth at 7AM :)

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Dec 20 '05 #4
thanks to all. ivo -> i will try your approach and see if it helps. in
the meantime i am still looking for any other reference to this focus
problem.

thanks to all you geniuses for help. any more feedback appreciated.

Dec 20 '05 #5
ivo->tried your approach. but its not working. i get an error message
when i focus on the second text field - Object expected. can somebody
help.

Dec 20 '05 #6
i have done this so far,

function vk_data(x) {
var focused = document.forms[0].elements['usernameField'];
window.onload = function() {
document.forms[0].elements['usernameField'].onfocus() = function() {
alert("a");
focused = document.forms[0].elements['usernameField'];
}
document.forms[0].elements['passwordField'].onfocus() = function() {
focused = document.forms[0].elements['passwordField'];
alert("b");
}
}
focused.value+=x;
}

But it doesnt seem to work. I am calling vk_data on click of a button
(onClick) which is for diff characters. however its always focused
under username field and everytime any character button is pressed it
enters it in the username field. even if i click on password field it
still enters it within username field.

can somebody please show me how to enter in fields wherever the focus
is. thanks.

Dec 21 '05 #7
Ivo
<sc*****@gmail.com> wrote
i have done this so far,

function vk_data(x) {
var focused = document.forms[0].elements['usernameField'];
Earlier in the thread we said focused was going to be a global variable. By
using the var keyword here you make focused local to the vk_data function,
and its value, which we want to remember, will be lost when that function
returns.
window.onload = function() {
It 's certainly possible to assign event handlers over and over, but keeping
this onload setting in the global scope will prevent such waste. Move it out
of the function and it will run only once, just enough.

document.forms[0].elements['usernameField'].onfocus() = function() {
When setting onfocus or other handlers, don't use the brackets. They attempt
to make it a call to a method instead.
alert("a");
focused = document.forms[0].elements['usernameField'];
}
document.forms[0].elements['passwordField'].onfocus() = function() {
focused = document.forms[0].elements['passwordField'];


When event handlers are called directly like here, they have the 'this'
keyword available to them as a reference to the object that fired the event.
In other words, the above function needs no more than "focused = this".

See if that makes any differnce.
hth
ivo
http://4umi.com/web/javascript/
Dec 21 '05 #8
On 2005-12-20, sc*****@gmail.com <sc*****@gmail.com> wrote:
hi all,

i have a html form with multiple text fields, accepting input from a
virtual onscreen keyboard.

so far i have document.forms[0].elements['usernameField'].value+=x;

The problem is that i dont want to hard code a text field name. it
should type in text wherever the focus is.

can somebody suggest a generic solution. thanks.


store the focussed element to a global variable using the onfocus event of
each field.

Bye.
Jasen
Dec 21 '05 #9

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

Similar topics

0
by: Kim Petersen | last post by:
Is it possible in Tkinter to simulate a keystroke into the current active (focused widget) by pressing another button (this is for a virtual keyboard on a touchscreen pc). the troubles i see is:...
8
by: Yeah | last post by:
I wish to use a drop box where each Option will not take the user to a web page - but a certain location on the same page the drop box exists. For example, Option 1 would take the user to "Chapter...
1
by: Linus Sylv?n | last post by:
Hi I'm trying to make a virtual keyboard and having problems with the focusing and seeing to that the keyboard is always on top. If I don't care about the keyboard being on top and use the...
4
by: jonathandrott | last post by:
Hi, i'm kinda new to vb.net and i'm trying to make an on screen keyboard to use with a touch screen. how do i get it to print a letter on the screen when the button is pressed in the textbox that...
1
by: cvairetti | last post by:
Hi, I just write an application in C# that show an interface with five button, each button starts an external application that it comes in front of the main C# application with focus on. When the...
1
by: lolly | last post by:
hi i recently used a virtual keyboard from www.codeproject.com/jscript/jvk.asp. However this part of the code function keyb_callback(ch) { var text =...
7
by: Christopher Pisz | last post by:
My problem is my derived class is getting called twice instead of the base and then the derived. I thought this was the purpose for virtuals and dynamic casting :/ I want my base class to have its...
2
by: petinboy | last post by:
Hi All! I am developing an accessibility keyboard application with Visual Studio 2005 Professional Edition and with C#. The application is a keyboard with buttons that interacts with any...
8
by: BD | last post by:
How can I duplicate the behavior of the operating system shortcut keys in my application? For example, my windows form has 5 controls (textboxes), the operating system will pickup which control...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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...

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.