Menu

[7cef54]: / src / staticResources / PythonResources.java  Maximize  Restore  History

Download this file

99 lines (80 with data), 2.4 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
package staticResources;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.io.File;
import java.lang.reflect.Field;
import core.keyChain.KeyStroke;
import core.languageHandler.Language;
import utilities.FileUtility;
public class PythonResources extends AbstractNativeLanguageBootstrapResource {
@Override
protected boolean correctExtension(String name) {
return name.endsWith(".py");
}
@Override
protected boolean generateKeyCode() {
StringBuilder sb = new StringBuilder();
Field[] fields = KeyEvent.class.getFields();
for (Field f : fields) {
String name = f.getName();
if (!name.startsWith("VK_")) {
continue;
}
addCodeFromField(sb, f);
}
fields = InputEvent.class.getFields();
for (Field f : fields) {
String name = f.getName();
if (!name.startsWith("BUTTON") || !name.endsWith("MASK")) {
continue;
}
addCodeFromField(sb, f);
}
for (KeyStroke.Modifier modifier : KeyStroke.Modifier.values()) {
addCode(sb, modifier.name(), modifier.getValue());
}
String keyCodeFilePath = FileUtility.joinPath(getExtractingDest().getAbsolutePath(), "key_code.py");
return FileUtility.writeToFile(sb.toString().trim(), new File(keyCodeFilePath), false);
}
/**
* Add a new line of code that defines the field constant with field
* name and its integer value.
*/
private void addCode(StringBuilder sb, String name, int code) {
sb.append(name + " = " + code);
sb.append("\n");
}
/**
* Add a new line of code that defines the field constant.
* The new line of code will be appended to an input string builder.
*
* @param sb string builder to append the code to.
* @param f field containing the information about the constant to add.
*/
private void addCodeFromField(StringBuilder sb, Field f) {
try {
sb.append(f.getName() + " = " + f.getInt(KeyEvent.class));
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
sb.append("\n");
}
@Override
protected String getRelativeSourcePath() {
return "natives/python";
}
@Override
protected File getExtractingDest() {
return new File(FileUtility.joinPath("resources", "python"));
}
@Override
protected Language getLanguage() {
return Language.PYTHON;
}
@Override
public File getIPCClient() {
return new File("resources/python/repeat_lib.py");
}
}
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.