package test;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.filechooser.FileSystemView;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ItemEvent;
import java.io.*;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
/**
* 描述:
* 修改pc按键
*
* @author miweiqiang
* @create 2018-11-22 15:57
*/
public class Test003 extends JFrame {
private static JPanel contentPane;
public Test003() throws HeadlessException {
this.setTitle("键位修改器");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(500, 400);
this.setLocationRelativeTo(null);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
this.setContentPane(contentPane);
contentPane.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5));
this.setVisible(true);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Map<String, Object> keyMaps = getKeyMaps();
Test003 example = new Test003();
JLabel label = new JLabel("修改键位:");
contentPane.add(label);
JComboBox<String> comboBox = new JComboBox<>();
for (Map.Entry entry : keyMaps.entrySet()) {
String keyName = entry.getKey().toString();
if (!keyName.equals("None")) {
comboBox.addItem(keyName);
}
}
contentPane.add(comboBox);
JLabel label1 = new JLabel("为:");
contentPane.add(label1);
JComboBox<String> comboBox1 = new JComboBox<>();
for (Map.Entry entry : keyMaps.entrySet()) {
comboBox1.addItem(entry.getKey().toString());
}
contentPane.add(comboBox1);
JButton addBtn = new JButton("添加");
JButton creatBtn = new JButton("生成");
contentPane.add(addBtn);
contentPane.add(creatBtn);
JTextArea textArea = new JTextArea();
// textArea.setSize(new Dimension(nu));
textArea.setBackground(Color.WHITE);
contentPane.add(textArea);
Map<String, Object> replaceKeyValues = new LinkedHashMap<>(16);
addBtn.addActionListener(new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
textArea.append(comboBox.getSelectedItem() + " => " + comboBox1.getSelectedItem() + "\n");
replaceKeyValues.put(comboBox.getSelectedItem().toString(), comboBox1.getSelectedItem());
}
});
replaceKeyValues.put("Caps Lock", "Enter");
replaceKeyValues.put("Q", "A");
replaceKeyValues.put("W", "S");
replaceKeyValues.put("E", "D");
replaceKeyValues.put("R", "F");
// aleryKeyMaps(replaceKeyValues);
// recover();
contentPane.validate();
}
/**
* 恢复按键
*/
private static void recover() {
String recover = "@echo off\n" +
"reg delete \"hklm\\system\\currentcontrolset\\control\\keyboard layout\" /v \"ScanCode Map\" /f\n" +
"echo \"键位已恢复,重启系统后生效\"\n" +
"pause";
FileWriter fileWriter = null;
try {
fileWriter = new FileWriter(FileSystemView.getFileSystemView().getHomeDirectory() + File.separator + "恢复键位器.bat");
fileWriter.write(recover);
fileWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 修改按键
*
* @param replaceKeyValues .
*/
private static void aleryKeyMaps(Map<String, Object> replaceKeyValues) {
StringBuilder regeditNum = new StringBuilder();
String size = replaceKeyValues.size() + 1 + "";
regeditNum.append("Windows Registry Editor Version 5.00\n" +
" [HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Control\\Keyboard Layout] \n" +
" \"Scancode Map\"=hex:").append("0000000000000000").append(size.length() > 1 ? size : "0" + size).append("000000");
Map<String, Object> keyValues = getKeyMaps();
for (Map.Entry<String, Object> entry : replaceKeyValues.entrySet()) {
regeditNum.append(keyValues.get(entry.getValue().toString())).append(keyValues.get(entry.getKey().toString()));
}
regeditNum.append("00000000").toString().replace(" ", "");
try {
FileWriter fileWriter = new FileWriter(FileSystemView.getFileSystemView().getHomeDirectory() + File.separator + "键位修改器.bat");
fileWriter.write(regeditNum.toString());
fileWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
private static Map<String, Object> getKeyMaps() {
Map<String, Object> keyMaps = new LinkedHashMap<>(16);
keyMaps.put("None", "00 00");
keyMaps.put("Esc", "01 00");//即Esc键的扫描码是"0001"
keyMaps.put("1", "02 00");
keyMaps.put("2", "03 00");
keyMaps.put("3", "04 00");
keyMaps.put("4", "05 00");
keyMaps.put("5", "06 00");
keyMaps.put("6", "07 00");
keyMaps.put("7", "08 00");
keyMaps.put("8", "09 00");
keyMaps.put("9", "0a 00");
keyMaps.put("0", "0b 00");
keyMaps.put("-", "0c 00");
keyMaps.put("+", "0d 00");
keyMaps.put("Backspace", "0e 00");
keyMaps.put("Tab", "0f 00");
keyMaps.put("Q", "10 00");
keyMaps.put("W", "11 00");
keyMaps.put("E", "12 00");
keyMaps.put("R", "13 00");
keyMaps.put("T", "14 00");
keyMaps.put("Y", "15 00");
keyMaps.put("U", "16 00");
keyMaps.put("I", "17 00");
keyMaps.put("O", "18 00");
keyMaps.put("P", "19 00");
keyMaps.put("[", "1a 00");
keyMaps.put("]", "1b 00");
keyMaps.put("Enter", "1c 00");
keyMaps.put("Left Ctrl", "1d 00");
keyMaps.put("A", "1e 00");
keyMaps.put("S", "1f 00");
keyMaps.put("D", "20 00");
keyMaps.put("F", "21 00");
keyMaps.put("G", "22 00");
keyMaps.put("H", "23 00");
keyMaps.put("J", "24 00");
keyMaps.put("K", "25 00");
keyMaps.put("L", "26 00");
keyMaps.put(";", "27 00");
keyMaps.put("'", "28 00");
keyMaps.put("`", "29 00");
keyMaps.put("Left Shift", "2a 00");
keyMaps.put("\\", "2b 00");
keyMaps.put("Z", "2c 00");
keyMaps.put("X", "2d 00");
keyMaps.put("C", "2e 00");
keyMaps.put("V", "2f 00");
keyMaps.put("B", "30 00");
keyMaps.put("N", "31 00");
keyMaps.put("M", "32 00");
keyMaps.put(",", "33 00");
keyMaps.put(".", "34 00");
keyMaps.put("/", "35 00");
keyMaps.put("Right Shift", "36 00");
keyMaps.put("n*", "37 00");
keyMaps.put("Left Alt", "38 00");
keyMaps.put("Space", "39 00");
keyMaps.put("Caps Lock", "3a 00");
keyMaps.put("F1", "3b 00");
keyMaps.put("F2", "3c 00");
keyMaps.put("F3", "3d 00");
keyMaps.put("F4", "3e 00");
keyMaps.put("F5", "3f 00");
keyMaps.put("F6", "40 00");
keyMaps.put("F7", "41 00");
keyMaps.put("F8", "42 00");
keyMaps.put("F9", "43 00");
keyMaps.put("F10", "44 00");
keyMaps.put("Num Lock", "45 00");
keyMaps.put("Scroll Lock", "46 00");
keyMaps.put("n7", "47 00");
keyMaps.put("n8", "48 00");
keyMaps.put("n9", "49 00");
keyMaps.put("n-", "4a 00");
keyMaps.put("n4", "4b 00");
keyMaps.put("n5", "4c 00");
keyMaps.put("n6", "4d 00");
keyMaps.put("n+", "4e 00");
keyMaps.put("n1", "4f 00");
keyMaps.put("n2", "50 00");
keyMaps.put("n3", "51 00");
keyMaps.put("n0", "52 00");
keyMaps.put("n.", "53 00");
keyMaps.put("F11", "57 00");
keyMaps.put("F12", "58 00");
keyMaps.put("nEnter", "1c e0");
keyMaps.put("Right Ctrl", "1d e0");
keyMaps.put("PrtSc", "37 e0");
keyMaps.put("Right Alt", "38 e0");
keyMaps.put("Home", "47 e0");
keyMaps.put("Up", "48 e0");
keyMaps.put("Page Up", "49 e0");
keyMaps.put("Left", "4b e0");
keyMaps.put("Right", "4d e0");
keyMaps.put("End", "4f e0");
keyMaps.put("Down", "50 e0");
keyMaps.put("Page Down", "51 e0");
keyMaps.put("Insert", "52 e0");
keyMaps.put("Delete", "53 e0");
keyMaps.put("Left Windows", "5b e0");
keyMaps.put("Right Windows", "5c e0");
return keyMaps;
}
}