/    Sign up×
Community /Pin to ProfileBookmark

Virtual Keyboard Enter Button

Hello All,

Forgive in advance my lack of knowledge on this subject, but I think my problem is a relatively simple one for someone more familiar with Javascript.

I have a wonderful virtual keyboard for use on a touch screen kiosk. This keyboard comes in the form of a Firefox plugin ( I can’t seem to figure out how to get Firebug to “see” a plugin…) which consist of nothing more then an overlay.xul file and an overlay.js file.

Everything in this keyboard works well except for the “Enter” key. It works for adding a new line, but doesn’t work for submitting forms and things like that.

What I really would like to do is have the Enter button simply emulate actually striking the enter key. For example you type you search into Google and hit enter, your search is executed. My goal in this is to have the button universally submit forms that are written to submit when enter is struck.

Here is the overlay.js file:

[CODE]/*
FxKeyboard
Version: 1.2
Author: Marko Zabreznik
Date: 22 Feb 2009
Purpose: Firefox Keyboard
*/
window.addEventListener(“load”, function() { FxKeyboard.startUp(); }, false);
var FxKeyboard = {
startUp: function()
{
var self = this;
// globals —————————————*/
this.shift = false;
this.caps = false;
this.focus;
this.capsbutton = document.getElementById(‘fxkeyboard_caps’);
this.shiftbutton = document.getElementById(‘fxkeyboard_shift’);
this.toolbar = document.getElementById(‘fxkeyboard_toolbar’);
// draw layout —————————————*/
osKeys = new Array (
‘1’,’2′,’3′,’4′,’5′,’6′,’7′,’8′,’9′,’0′,’%’,’*’,’-‘,’ä’,-1,
‘q’,’w’,’e’,’r’,’t’,’z’,’u’,’i’,’o’,’p’,’š’,’đ’,’+’,’ß’,-1,
‘a’,’s’,’d’,’f’,’g’,’h’,’j’,’k’,’l’,’č’,’ć’,’ž’,’€’,’ö’,-1,
‘y’,’x’,’c’,’v’,’b’,’n’,’m’,’@’,’.’,’,’,’!’,’?’,’\’,’ü’);
var keyboard = document.getElementById(‘fxkeyboard_keyboard’);
var tempRow = document.createElement(“hbox”);var temp;
for ( var i in osKeys ) {
if (osKeys[i] == -1) {
tempRow.setAttribute(“flex”, ‘1’);
keyboard.appendChild(tempRow);
tempRow = document.createElement(“hbox”);
}
else {
tempRow.appendChild(this.addButton(osKeys[i].toUpperCase(),osKeys[i],osKeys[i].toUpperCase()));
}
}
tempRow.setAttribute(“flex”, ‘1’);
keyboard.appendChild(tempRow);

// on click —————————————*/
window.addEventListener(“DOMContentLoaded”, function(aEvent) {
var thebrowserget = gBrowser.getBrowserForDocument(aEvent.target);
if(thebrowserget)thebrowserget.addEventListener(“click”, function() { FxKeyboard.itemFocused(); }, false);
FxKeyboard.toggletoolbar(true);
}, true);
},
itemFocused: function () {
var focused = document.commandDispatcher.focusedElement;
if (focused && (focused.type == ‘text’ || focused.type == ‘textarea’ || focused.type == “password”)) {
// textarea found, open keyboard
this.focus = focused;
this.toggletoolbar(false);
}
else {
this.toggletoolbar(true);
}
},
toggletoolbar: function(set){
this.toolbar.collapsed = set;
},
buttonAction: function(button) {
if (this.shift) {
this.focus.value+=(button.action2);
this.doShift();
}
else
this.focus.value+=(button.action1);
},
addButton: function(name, action1, action2) {
tempButton = document.createElement(“button”);
tempButton.setAttribute(“label”, name);
tempButton.setAttribute(“tooltiptext”, name);
tempButton.setAttribute(“flex”, ‘1’);
tempButton.action1 = action1;
// simple test if it is a character
if (name.toLowerCase() !== name.toUpperCase()) tempButton.style.fontWeight=”bold”
tempButton.style.minWidth=”40px”
tempButton.style.height=”40px”
tempButton.action2 = action2;
tempButton.setAttribute(“oncommand”, ‘FxKeyboard.buttonAction(this)’);
return tempButton;
},
doShift: function (){
if (this.caps) return;
this.shift = !this.shift;
this.shiftbutton.style.color = (this.shift) ? “#ff0000” : ‘#000000’;
},
doCaps: function (){
if (this.caps) {
this.shift = this.caps = false;
this.capsbutton.style.color = this.shiftbutton.style.color = ‘#000000’;
}
else {
this.shift = this.caps = true;
this.capsbutton.style.color = this.shiftbutton.style.color = ‘#ff0000’;
}
},
doBackspace: function (){
this.focus.value = this.focus.value.slice(0, -1);
},
doClear: function (){
this.focus.value=”;
},
doEnter: function (){
this.focus.value+=’n’;
},
doSpace: function (){
this.focus.value+=’ ‘;
}
}
// END FxKeyboard[/CODE]

If anyone can help I thank you very much in advance. If no one has any ideas, even a point in the right direction would be greatly appreciated.

to post a comment
JavaScript

8 Comments(s)

Copy linkTweet thisAlerts:
@mavalos88Mar 26.2010 — At the very bottom you have this function
[CODE]
doEnter: function (){
this.focus.value+='n';
}
[/CODE]


What this is doing is adding a carriage return, or a new line as you said.

You'd have to change that line so it does what you want it to do. If it is to submit the form on your page then it would be somewhere along the lines of "document.myForm.submit();" assuming there is no submit button on your page. If there is then this won't work.

If your page does have a submit button, I'd recommend changing it to a regular button and onclick call a function that submits the form using document.myForm.submit();.
Copy linkTweet thisAlerts:
@sagediningauthorMar 26.2010 — Hello mavalos88,

Thank you for your response. I'm looking to do something similar to what you mentioned, but I need it to be a bit more flexible. Is it possible to do some kind of wildcard value for "myForm" to make it submit whatever form has focus at the moment?

Could I possibly do:

[CODE]this.focus.value= "document.myForm.submit();"[/CODE]

Could this work?
Copy linkTweet thisAlerts:
@mavalos88Mar 26.2010 — Well by definition the submit action will send everything inside the <form> </form> tags. To do what I'm understanding you need, you'd have to enclose every element you want to submit at some point inside their own <form> tag.

Your submit function will have to receive the active control as a parameter and from that decide which form to submit.

Although you could just submit everything and on your other page just grab the value you are interested in.

Your other option is to submit using the get method. That way instead of submitting the form you could do something like:

[CODE]location.href="mypage.php?ActiveControl="+controlValue;[/CODE]

Then you'd just need to pass the value of the active control to construct your link.

If you are just using plain html then you'll need to make a function that retrieves the values from your link. Javascript doesn't do that natively so you'll need to make your own function to retrieve values from your link.

Tutorial: http://aarongough.com/content/view/48/1/

This is just a layout of how you can do it but you'll need to read more on how to use Javascript.

Hope that helps.
Copy linkTweet thisAlerts:
@rnd_meMar 27.2010 — 
What I really would like to do is have the Enter button simply emulate actually striking the enter key. [/QUOTE]


for firefox:
[CODE]
function simKey() {
var A=[].slice.call(arguments),
a1=A[0]||"",
base= document.body;
if(a1.nodeName){ base=a1;A.splice(0,1);a1=A[0]}
if(a1 && a1.split && a1.length>1){A=a1.split("");}
if(a1.splice){ A=a1; }
A.map(function(key){
var ev = document.createEvent("KeyEvents");
ev.initKeyEvent("keypress",
true,true,window,false,false,false,false,key.toFixed?key:key.charCodeAt(0),key.toFixed?key:key.charCodeAt(0));
base.dispatchEvent(ev);
});
}

//firebug example:
var tb=$$("textarea")[0];

simKey(tb, "hello world");
simKey(tb, 13);
simKey(tb, "hello world".split(""));


// //////////////
doEnter: function (){
simKey(this.focus, 13);
}[/CODE]

usage:

there's an optional first argument: element to receive keypress.

for subsequent argument(s): string chars, number key codes, a long string of chars as one argument, or an array of one-character strings or char code numbers.



it might simplify other parts of your code as well...
Copy linkTweet thisAlerts:
@sagediningauthorMar 30.2010 — rnd me,

The direction you're going I believe is the correct one and I thank you very much for the input. I did see threads elsewhere about people using these codes to have keys imitate other keys; For example having the Alt key imitate Enter, and I thought we could use this.

The problem is that being a plugin, I think this keyboard is one huge function by the look of the code. Here is the beginning of the overlay file:

[CODE]window.addEventListener("load", function() { FxKeyboard.startUp(); }, false);
var FxKeyboard = {

startUp: function()
{
var self = this;
// globals ---------------------------------------*/
this.shift = false;
this.caps = false;
this.focus;
function simKey() {
var A=[].slice.call(arguments),
a1=A[0]||"",
base= document.body;
if(a1.nodeName){ base=a1;A.splice(0,1);a1=A[0]}
if(a1 && a1.split && a1.length>1){A=a1.split("");}
if(a1.splice){ A=a1; }
A.map(function(key){
var ev = document.createEvent("KeyEvents");
ev.initKeyEvent("keypress",
true,true,window,false,false,false,false,key.toFixed?key:key.charCodeAt(0),key.toFixed?key:key.charCodeAt(0));
base.dispatchEvent(ev);
});
}
this.capsbutton = document.getElementById('fxkeyboard_caps');
this.shiftbutton = document.getElementById('fxkeyboard_shift');
this.toolbar = document.getElementById('fxkeyboard_toolbar');[/CODE]


I tried to drop your simKey function in with the "globals", as this is the only place I could stick it without it causing the keyboard to not function at all anymore. Is there a way to put this whole function into "doEnter"?

The function works great, but the layout of the overlay file seems very unfriendly to any functions outside its own...

Thanks again to all who have contributed.
Copy linkTweet thisAlerts:
@rnd_meMar 31.2010 — it's getting locked into the scope of that first function, startup.

if it works there, that's cool, but you need to make it global.

no biggie, just publish the function by making it a global property:

replace this line:
[CODE]function simKey() {[/CODE]

with these two, which look out of order:
[CODE] window.simKey=simkey;
function simKey() {[/CODE]


viola, simKey should now be accessible from anywhere, including the doEnter method at the bottom.
Copy linkTweet thisAlerts:
@sagediningauthorApr 06.2010 — rnd me,

I thank you very much for your patience with me. Altering the code you suggested has brought me back to the keyboard not displaying anymore when an input area is selected. I'm really at a loss here. Could this be caused by it being a Firefox plugin and not just a plain js file? Due to this I can't really use Firebug to see where its failing so I'm just lost. Here is the entire overlay.js file if you can even stand to look at it again.

[CODE]
FxKeyboard
Version: 1.2
Author: Marko Zabreznik
Date: 22 Feb 2009
Purpose: Firefox Keyboard
*/
window.addEventListener("load", function() { FxKeyboard.startUp(); }, false);
var FxKeyboard = {
startUp: function()
{
var self = this;
// globals ---------------------------------------*/
this.shift = false;
this.caps = false;
this.focus;
window.simKey=simkey;
function simKey() {
var A=[].slice.call(arguments),
a1=A[0]||"",
base= document.body;
if(a1.nodeName){ base=a1;A.splice(0,1);a1=A[0]}
if(a1 && a1.split && a1.length>1){A=a1.split("");}
if(a1.splice){ A=a1; }
A.map(function(key){
var ev = document.createEvent("KeyEvents");
ev.initKeyEvent("keypress",
true,true,window,false,false,false,false,key.toFixed?key:key.charCodeAt(0),key.toFixed?key:key.charCodeAt(0));
base.dispatchEvent(ev);
});
}
this.capsbutton = document.getElementById('fxkeyboard_caps');
this.shiftbutton = document.getElementById('fxkeyboard_shift');
this.toolbar = document.getElementById('fxkeyboard_toolbar');
// draw layout ---------------------------------------*/
osKeys = new Array (
'1','2','3','4','5','6','7','8','9','0','-','=','+','\',-1,
'q','w','e','r','t','y','u','i','o','p','@','$','&#37;','#',-1,
'a','s','d','f','g','h','j','k','l',';',':',''','"','`',-1,
'z','x','c','v','b','n','m',',','.','/','!','?','<','>');
var keyboard = document.getElementById('fxkeyboard_keyboard');
var tempRow = document.createElement("hbox");var temp;
for ( var i in osKeys ) {
if (osKeys[i] == -1) {
tempRow.setAttribute("flex", '1');
keyboard.appendChild(tempRow);
tempRow = document.createElement("hbox");
}
else {
tempRow.appendChild(this.addButton(osKeys[i].toUpperCase(),osKeys[i],osKeys[i].toUpperCase()));
}
}
tempRow.setAttribute("flex", '1');
keyboard.appendChild(tempRow);

// on click ---------------------------------------*/
window.addEventListener("DOMContentLoaded", function(aEvent) {
var thebrowserget = gBrowser.getBrowserForDocument(aEvent.target);
if(thebrowserget)thebrowserget.addEventListener("click", function() { FxKeyboard.itemFocused(); }, false);
FxKeyboard.toggletoolbar(true);
}, true);
},
itemFocused: function () {
var focused = document.commandDispatcher.focusedElement;
if (focused && (focused.type == 'text' || focused.type == 'textarea' || focused.type == "password")) {
// textarea found, open keyboard
this.focus = focused;
this.toggletoolbar(false);
}
else {
this.toggletoolbar(true);
}
},
toggletoolbar: function(set){
this.toolbar.collapsed = set;
},
buttonAction: function(button) {
if (this.shift) {
this.focus.value+=(button.action2);
this.doShift();
}
else
this.focus.value+=(button.action1);
},
addButton: function(name, action1, action2) {
tempButton = document.createElement("button");
tempButton.setAttribute("label", name);
tempButton.setAttribute("tooltiptext", name);
tempButton.setAttribute("flex", '1');
tempButton.action1 = action1;
// simple test if it is a character
if (name.toLowerCase() !== name.toUpperCase()) tempButton.style.fontWeight="bold"
tempButton.style.minWidth="40px"
tempButton.style.height="40px"
tempButton.action2 = action2;
tempButton.setAttribute("oncommand", 'FxKeyboard.buttonAction(this)');
return tempButton;
},
doShift: function (){
if (this.caps) return;
this.shift = !this.shift;
this.shiftbutton.style.color = (this.shift) ? "#ff0000" : '#000000';
},
doCaps: function (){
if (this.caps) {
this.shift = this.caps = false;
this.capsbutton.style.color = this.shiftbutton.style.color = '#000000';
}
else {
this.shift = this.caps = true;
this.capsbutton.style.color = this.shiftbutton.style.color = '#ff0000';
}
},
doBackspace: function (){
this.focus.value = this.focus.value.slice(0, -1);
},
doClear: function (){
this.focus.value='';
},
doEnter: function (){
simKey(this.focus, 13);
},
doSpace: function (){
this.focus.value+=' ';
}
}
// END FxKeyboard
[/CODE]


Thanks again for all your help up to this point.
Copy linkTweet thisAlerts:
@rnd_meApr 06.2010 — i'd really have to see the corresponding html to figure out the problem.

you can send me a private message with a live link if that's convenient for you.
×

Success!

Help @sagedining spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...