键盘布局库中查找特定输入键周边按键(JAVA的preferable) [英] Keyboard Layout library to find Neighboring Keys given an input key (java preferable)
问题描述
有谁知道一个图书馆(preferably JAVA),可以给我钥匙邻国给予美国英语标准键盘键输入?
Does anyone know of a library (preferably java) that can give me neighboring keys given a key input for US_ENGLISH standard keyboard?
例如。如果我输入的字符'D',我应该得到以下字符返回:[W,E,R,S,F,X,C,V]
E.g. if I input the character 'd', I should get the following characters returned: [w,e,r,s,f,x,c,v].
另外一个网格操作API将工作太(这样我就可以实例化一个QWERTY键盘布局的网格,用它来寻找我的邻居)。
Alternatively a grid manipulation api would work too (so that I can instantiate a grid with a qwerty keyboard layout and use it to find my neighbors).
注1:我现在用的词字符和关键的同义词是指字符
Note 1: I am using the words 'character' and 'key' synonymously to refer to characters.
注2:我知道我可以硬code到50或那么主键映射到他们的邻居的方法。我要寻找一个更好的解决方案。
Note 2: I know I can hard-code a method to map the 50-or-so primary keys to their neighbors. I am looking for a better solution.
感谢你。
推荐答案
我同意BlueNovember,但如果你想要更多的数学解决方案来检查。首先创建角色的网格,然后返回他们都具有从传递...有点儿无用的关键的距离小于2,但它是一个不错的练习:)
I agree with BlueNovember but in case you want a more "mathematical" solution check this. First you create a grid of characters and then you return all of them that has a distance smaller than 2 from the key passed... Kinda useless but it's a nice exercise :)
public class Test {
static String chars = "qwertyuiopasdfghjkl;zxcvbnm,.";
public static void main(String[] args) {
System.out.println(getNeighboringKeys('f'));
System.out.println(getNeighboringKeys('u'));
System.out.println(getNeighboringKeys('m'));
System.out.println(getNeighboringKeys('q'));
}
public static String getNeighboringKeys(char key) {
StringBuffer result = new StringBuffer();
for (char c : chars.toCharArray()) {
if (c!=key && distance(c,key)<2) {
result.append(c);
}
}
return result.toString();
}
public static double distance(char c1, char c2) {
return Math.sqrt(Math.pow(colOf(c2)-colOf(c1),2)+Math.pow(rowOf(c2)-rowOf(c1),2));
}
public static int rowOf(char c) {
return chars.indexOf(c) / 10;
}
public static int colOf(char c) {
return chars.indexOf(c) % 10;
}
}
这篇关于键盘布局库中查找特定输入键周边按键(JAVA的preferable)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!