KeyUp.java
4.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package cn.csbr.app.util;
import javafx.beans.value.ChangeListener;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import org.controlsfx.control.textfield.CustomTextField;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.reflect.Field;
/**
*键盘弹出工具类
* 小白
*/
public class KeyUp {
private static Process process;
/**
* 为指定对象的TextField增加获取焦点时弹出小键盘
* @param s 对象
*/
public static void setTextFieldOrPasswordField(Object s,boolean iswin7){
Runtime runtime = Runtime.getRuntime();
ChangeListener<Boolean> changeListener= (observable, oldValue, newValue) -> {
if (newValue) {
try {
// System.out.println("打开键盘");
// if(process!=null) {
// process.destroyForcibly();
// }
System.out.println(oldValue+":"+newValue);
if(!iswin7) {
if (!findProcess("osk.exe")) {
process = runtime.exec("cmd /c start" + "C:\\osk.exe");
}
}else {
if (!findProcess("tabtip.exe")) {
process = runtime.exec("cmd /c start " + "tabtip.exe");
} else {
System.out.println("键盘已打开!");
}
}
} catch (IOException e) {
e.printStackTrace( );
}
}
};
if(s.getClass().equals(TextField.class) ){
// ( (TextField)s).focusedProperty().addListener(e->{""});
( (TextField)s).focusedProperty().addListener(changeListener);
}else if( s.getClass().equals(PasswordField.class)) {
( (PasswordField)s).focusedProperty().addListener(changeListener);
}else if( s.getClass().equals(CustomTextField.class)) {
( (CustomTextField)s).focusedProperty().addListener(changeListener);
}
else {
Field[] fields = s.getClass( ).getDeclaredFields( );
for (int i = 0; i < fields.length; i++) {
if (fields[i].getType( ).equals(TextField.class)) {
fields[i].setAccessible(true);
try {
TextField textField = (TextField) fields[i].get(s);
System.out.println(fields[i].getName( ) + "开始绑定键盘");
textField.focusedProperty( ).addListener(changeListener);
} catch (IllegalAccessException e) {
e.printStackTrace( );
}
fields[i].setAccessible(false);
} else if (fields[i].getType( ).equals(PasswordField.class)) {
fields[i].setAccessible(true);
try {
TextField textField = (TextField) fields[i].get(s);
textField.focusedProperty( ).addListener(changeListener);
} catch (IllegalAccessException e) {
e.printStackTrace( );
}
fields[i].setAccessible(false);
}
else if (fields[i].getType( ).equals(CustomTextField.class)) {
fields[i].setAccessible(true);
try {
CustomTextField textField = (CustomTextField) fields[i].get(s);
textField.focusedProperty( ).addListener(changeListener);
} catch (IllegalAccessException e) {
e.printStackTrace( );
}
fields[i].setAccessible(false);
}
}
}
}
public static boolean findProcess(String name) {
BufferedReader bufferedReader = null;
try {
Process proc = Runtime.getRuntime().exec("tasklist -fi " + '"' + "imagename eq " + name +'"');
bufferedReader = new BufferedReader(new InputStreamReader(proc.getInputStream()));
String line = null;
while ((line = bufferedReader.readLine()) != null) {
if (line.contains(name)) {
return true;
}
}
return false;
} catch (Exception ex) {
ex.printStackTrace();
return false;
} finally {
if (bufferedReader != null) {
try {
bufferedReader.close();
} catch (Exception ex) {}
}
}
}
}