开发者

transform pressed key values to english values

I have an input like this

<input id="guestCard" name="guestCard" onkeypress="transform(event)" type="text" value="" />

and I want to transform the key pressed on a keyboard regardless of which language settings to english character. E.g. when I press on czech keyboard + (with keycode 43) , I want to get 1 (with keycode 49). Is there some standard way to do this? How would the function transform look like?

Alternatively I want the same functionality but using ajax (on asp.net开发者_运维技巧 mvc). Any ideas there?

Thanks in advance.


As far as I am aware, JavaScript is not locale aware - so you would need to somehow detect or have the user pick the appropriate transform mapping (in this case, perhaps a radio button for czech as the source and U.S. ASCII as the destination). Once that is taken care of, your function could be something like:

function transform(event) {
  var code = (event.charCode) ? event.charCode : event.keyCode; // cross-browser happy
  switch (code) {
    case 43 : return "1";
  }
}

There is a great test page to see how keyCode/charCode properties and the onKeyDown/Press/Up events behave in different browsers. http://asquare.net/javascript/tests/KeyCode.html


I doubt there is one but to create it, create an associative array, add some JS to a text field which saves the two values in the array and then press every key on the keyboard. After that, you can dump the array somewhere and use this as a constant in your code.

But be warned: Almost all users will have problems when they don't get the character on the screen which they've typed on the keyboard.


Trimack -

I think you are using the wrong event. You need onkeydown, and use the keyCode property of event.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜