1

i am making an onscreen keyboard, im stuck making codes with capslock key and shift key..anyone?

heres my code:

function number_write(x){
    var text_box = document.getElementById("number");
    if(x=='tab')
        text_box.value=text_box.value+'\t';
    else if(x=='enter')
        text_box.value=text_box.value+'\n';
    else if(x=='backspace')
        text_box.value=text_box.value.slice(0,-1);
    else text_box.value = text_box.value+x;
}

can you please add that code for caps lock and shift?thanks...

Vegar
  • 12,828
  • 16
  • 85
  • 151

2 Answers2

1

Are you looking for this (assuming x is the key event)

x.shiftKey
x.ctrlKey
x.altKey

They are pretty googleable

http://javascript.about.com/od/byexample/a/events-keymodifier-example.htm

if (x.which==bla && x.shiftKey)...
mplungjan
  • 169,008
  • 28
  • 173
  • 236
  • please bear with me im only a beginner...wat do you mean?if i press capslock, convert to uppercase..and also with the shift key converts then it includes the symbols..only capslock and left/right shift key only..thanks – Christian Busaco Jan 08 '13 at 07:33
0

You are searching for a String.prototype.toUpperCase()

Example:

var x = 's';
x.toUpperCase();
console.log(x); // 'S'

That's the CapsLock/Shift "function".

But you are reinventing the wheel.

Looks like you are checking in a element to pass for a function that will change the value in your input element.

So if you want to do a "keyboard" will be easier if you use Event Triggers from Javascript.

Check this post about stimulation keys.

Now you can just map your "virtual keyboard" to trigger key simulations in other element.

For CapsLock you will need to use the force it parameters in your event.

Here is the reference for keydown event. So you can construct your own, using or not, shift/CapsLock/Alt...

Finally you can search for Javascript Virtual Keyboard on google, you may find projects like this: https://github.com/cantrell/VirtualKeyboard

Community
  • 1
  • 1
Gabriel Gartz
  • 2,840
  • 22
  • 24