Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package mouamle.project.autoTranslater;
- import java.awt.Toolkit;
- import java.awt.datatransfer.DataFlavor;
- import java.net.URL;
- import java.net.URLEncoder;
- import java.util.concurrent.LinkedBlockingQueue;
- import org.jsoup.Jsoup;
- import org.jsoup.nodes.Document;
- import org.jsoup.nodes.Element;
- import lc.kra.system.keyboard.GlobalKeyboardHook;
- import lc.kra.system.keyboard.event.GlobalKeyAdapter;
- import lc.kra.system.keyboard.event.GlobalKeyEvent;
- public class KeyListener {
- @SuppressWarnings("deprecation")
- public static TranslationResult getTranslation(String text) {
- try {
- Document doc = Jsoup.parse(new URL(String.format("https://www.urbandictionary.com/define.php?term=%s", URLEncoder.encode(text))), 5000);
- Element el = doc.select(".def-panel").get(0);
- TranslationResult res = new TranslationResult(el.select(".def-header").text().trim(), el.select(".meaning").text().trim(), el.select(".example").text().trim());
- System.out.println(res);
- return res;
- } catch (Exception e) {}
- return null;
- }
- public static void showTranslate(String text) {
- getTranslation(text);
- }
- public static void main(String[] args) throws Exception {
- GlobalKeyboardHook keyboardHook = new GlobalKeyboardHook(true);
- LinkedBlockingQueue<String> toTranslate = new LinkedBlockingQueue<>(10);
- keyboardHook.addKeyListener(new GlobalKeyAdapter() {
- public void keyReleased(GlobalKeyEvent e) {
- if (e.getVirtualKeyCode() == GlobalKeyEvent.VK_C && e.isControlPressed()) {
- try {
- toTranslate.put((String) Toolkit.getDefaultToolkit().getSystemClipboard().getData(DataFlavor.stringFlavor));
- } catch (Exception e2) {}
- } else if (e.getVirtualKeyCode() == GlobalKeyEvent.VK_ESCAPE) {
- keyboardHook.shutdownHook();
- System.exit(0);
- }
- }
- });
- new Thread(() -> {
- while (true) {
- String word = toTranslate.poll();
- if (word != null) {
- showTranslate(word);
- }
- }
- }).start();
- }
- }
- class TranslationResult {
- public String title, desc, example;
- public TranslationResult() {}
- public TranslationResult(String title, String desc, String example) {
- this.title = title;
- this.desc = desc;
- this.example = example;
- }
- @Override
- public String toString() {
- return title + "\n" + desc + "\n\n" + example;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement