使用 javascript 更改键盘布局

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11426969/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 13:10:35  来源:igfitidea点击:

change keyboard layout with javascript

javascriptkeyboard-events

提问by hd.

I have a html form. Users can fill in the form in both english and persian languages. but I have a captcha input that users should fill it in english.

我有一个 html 表单。用户可以用英语和波斯语填写表格。但我有一个验证码输入,用户应该用英文填写。

If the user's keyboard layout is persian what is typed in this field should change to english so I need some coded that change the keyboard layout on focusing on this input text.

如果用户的键盘布局是波斯语,则在此字段中键入的内容应更改为英语,因此我需要一些代码来更改键盘布局以专注于此输入文本。

Is it possbile to change keyboard layout with javascript??

是否可以使用 javascript 更改键盘布局?

采纳答案by Max Girkens

You won't be able to change the keyboard layout using JS, but you can capture the keydown event and replace the character with something like this:

您将无法使用 JS 更改键盘布局,但您可以捕获 keydown 事件并将字符替换为以下内容:

http://jsfiddle.net/SxdKZ/

http://jsfiddle.net/SxdKZ/

$('textarea').on('keydown', function(e){

   console.log(e.keyCode); 
    if( e.keyCode == 90 ){
      e.preventDefault();
      $(this).append('y').focus();
    }
    if( e.keyCode == 89 ){
      e.preventDefault();
      $(this).append('z').focus();
    }

});?

回答by Umair Qureshi

I have made the default language of all the input text fields to Arabic. If you want to use English language for any text field, you will have to assign a class "en" to that particular text field.

我已将所有输入文本字段的默认语言设为阿拉伯语。如果您想对任何文本字段使用英语,则必须为该特定文本字段分配一个类“en”。

Just copy the following code and also download the js file from http://farsitype.ir/FarsiType.js

只需复制以下代码并从http://farsitype.ir/FarsiType.js下载 js 文件

/******** Make all the input text fields arabic ******/
$('input[type=text]').each(function(index, value){
 if (value.className != 'en') {
  $(this).attr('Lang','fa');
 }
});