Skip to content

Instantly share code, notes, and snippets.

@ayaysir
Created October 14, 2018 14:00
Show Gist options
  • Save ayaysir/0755aa402a1d5db5618df842d81c6fb0 to your computer and use it in GitHub Desktop.
Save ayaysir/0755aa402a1d5db5618df842d81c6fb0 to your computer and use it in GitHub Desktop.
package com.apple.hangeul;
import java.awt.AWTException;
import java.awt.EventQueue;
import java.awt.Robot;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.im.InputContext;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
public class AutoEnter extends JFrame {
private static final long serialVersionUID = 1l;
private JPanel contentPane;
private static String OS = System.getProperty("os.name").toLowerCase();
private static long inputDelay = 5000; // 인풋 간격
private static int inputKeyIndex = 0;
// new String[] {"엔터", "마침표(.)", "스페이스( )", "탭(\t)"}));
private static boolean isPaused = false;
private JTextField textField;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
AutoEnter frame = new AutoEnter();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public AutoEnter() {
setType(Type.UTILITY);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 173, 138);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lblInputContext = new JLabel("..");
lblInputContext.setBounds(5, 5, 162, 24);
contentPane.add(lblInputContext);
textField = new JTextField();
textField.setText("5000");
textField.setBounds(5, 38, 86, 26);
contentPane.add(textField);
textField.setColumns(10);
JButton btnNewButton = new JButton("변경");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
inputDelay = Long.parseLong(textField.getText());
} catch(Exception ex) {
JOptionPane.showMessageDialog(null,
"숫자만 밀리세컨드 단위로 입력하세요.", "에러",
JOptionPane.ERROR_MESSAGE);
}
}
});
btnNewButton.setBounds(93, 38, 75, 29);
contentPane.add(btnNewButton);
JComboBox<String> comboBox = new JComboBox<>();
comboBox.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println(comboBox.getSelectedIndex());
inputKeyIndex = comboBox.getSelectedIndex();
}
});
comboBox.setModel(new DefaultComboBoxModel<String>(
new String[] {"엔터", "마침표(.)", "스페이스( )", "탭(\t)"}));
comboBox.setBounds(5, 59, 161, 27);
contentPane.add(comboBox);
JButton btnPause = new JButton("TO PAUSE");
btnPause.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(!isPaused) {
System.out.println("PAUSED");
isPaused = true;
btnPause.setText("TO RESUME");
} else {
System.out.println("RESUMED");
isPaused = false;
btnPause.setText("TO PAUSE");
}
}
});
btnPause.setBounds(15, 87, 151, 29);
contentPane.add(btnPause);
setAlwaysOnTop(true);
// 스레드 1은 os를 판별하고 입력기를 프레임에 표시하는 역할을 한다.
new Thread() {
@Override
public void run() {
//== core ==//
System.out.println(OS.equalsIgnoreCase("mac os x"));
// 하이시에라의 경우 mac os x라고 뜬다.
if(OS.equalsIgnoreCase("mac os x")) {
try {
String line;
Process p = Runtime.getRuntime().exec("ps");
BufferedReader input =
new BufferedReader(new InputStreamReader(p.getInputStream()));
while ((line = input.readLine()) != null) {
System.out.println(line); //<-- Parse data here.
}
input.close();
while(true) {
// 현재 os의 입력기 코드를 읽어온다. 한글은 ko_KR
InputContext it = InputContext.getInstance();
// System.out.println(it.getLocale());
if(it.getLocale().toString().equalsIgnoreCase("ko_KR")) {
lblInputContext.setText(it.getLocale().toString());
}
else {
lblInputContext.setText(it.getLocale().toString());
}
}
} catch(Exception e) {
}
} else {
// 맥 os가 아닌 경우 프로그램을 종료한다.
System.exit(1);
}
}
}.start();
try {
// 맥이라면 스레드 2를 실행한다.
inputEnter();
} catch (AWTException e1) {
e1.printStackTrace();
}
}
// 스레드 2
public void inputEnter() throws AWTException {
Robot robot = new Robot(); // 가상 키 입력
new Thread() {
@Override
public void run() {
while(true) {
try {
// 맨 위에 선언된 인풋딜레이 변수만큼 대기한다.
Thread.sleep(inputDelay);
} catch (InterruptedException e) {
e.printStackTrace();
}
InputContext it = InputContext.getInstance();
// 입력기가 한글이라면
if(it.getLocale().toString().equalsIgnoreCase("ko_KR")) {
if(isPaused) return;
System.out.println("==PRESS==");
int keyEventNum = KeyEvent.VK_ENTER;
switch(inputKeyIndex) {
// 맨 위에 선언된 인풋키인덱스에서 숫자를 가져와서
// 그 숫자에 대응되는 키를 실행한다.
case 0:
keyEventNum = KeyEvent.VK_ENTER; break;
case 1:
keyEventNum = KeyEvent.VK_PERIOD; break;
case 2:
keyEventNum = KeyEvent.VK_SPACE; break;
case 3:
keyEventNum = KeyEvent.VK_TAB; break;
}
// 위 케이스에서 선택된 키를 눌렀다 뗀다.
robot.keyPress(keyEventNum);
robot.keyRelease(keyEventNum);
}
}
}
}.start();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment