Jump to content

Keyboard Macro Like A Baws


liverare

Recommended Posts

Looky what I found - forgot about this script! 

import java.awt.EventQueue;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import org.osbot.engine.Bot;
import org.osbot.engine.canvas.BotCanvas;
import org.osbot.script.MethodProvider;
import org.osbot.script.Script;
import org.osbot.script.ScriptManifest;

@ScriptManifest(name = "Keyboard Macro", info = "Allows for easy keyboard entry.", version = 1.0, author = "LiveRare")
public class Main extends Script implements MouseWheelListener, KeyListener {

	public static final String[] DEFAULT_TEXT = {
			"Hi, I'm a placeholder example text!",
			"Yo dude, same here!"};

	View view;

	boolean typing;
	BotCanvas bc;

	private volatile String curString;

	private final Runnable runSentence = new Runnable() {
		@Override
		public void run() {

			try {
				
				for (char next : curString.toCharArray())
					typeChar(next);

			} catch (InterruptedException e) {
				e.printStackTrace();
			}

		}
	};

	@Override
	public void onStart() {

		EventQueue.invokeLater(new Runnable() {

			@Override
			public void run() {

				view = new View() {

					private static final long serialVersionUID = 1L;

					@Override
					public void submitRequest(final String text) {
						Main.this.typeText(text);
					}
				};

				view.addText(DEFAULT_TEXT);
			}
		});

		useDefaultPaint(false);
		
		
		
		bc = getBotCanvas();
		
		if (bc != null) {
			bc.addMouseWheelListener(this);
			bc.addKeyListener(this);
		}
	}

	@Override
	public void onExit() throws InterruptedException {
		EventQueue.invokeLater(new Runnable() {
			@Override
			public void run() {
				view.dispose();
				
				if (bc != null) {
					bc.removeMouseWheelListener(Main.this);
					bc.removeKeyListener(Main.this);
				}
			}
		});
	}

	@Override
	public void mouseWheelMoved(final MouseWheelEvent e) {
		EventQueue.invokeLater(new Runnable() {
			@Override
			public void run() {
				switch(e.getWheelRotation()) {
				case -1: // up
					if (view != null)
						view.moveSelectionUp();
					break;
				case 1: // down
					if (view != null)
						view.moveSelectionDown();
					break;
				}
			}
		});
		
	}
	
	@Override
	public void keyPressed(KeyEvent e) {
		// TODO Auto-generated method stub
		
	}
	
	@Override
	public void keyReleased(KeyEvent e) {
		// TODO Auto-generated method stub
		
	}
	
	@Override
	public void keyTyped(KeyEvent e) {
		
		switch (e.getKeyChar()) {
		case KeyEvent.VK_TAB:
			if (view != null)
				view.forceSubmit();

			break;
		}
				
	}
	
	public void typeText(String text) {
		curString = text + "\n\n\n";
		new Thread(runSentence).start();
		
	}

	public synchronized void typeChar(char c) throws InterruptedException {
		try {
			bot.getKeyboard().typeKeyEvent(c, KeyEvent.KEY_TYPED);
		} finally {
			Thread.sleep(MethodProvider.random(10, 30));
		}
	}
	
	public synchronized void typeSpecialChar(int charCode) {
		try {
			Thread.sleep(50);
			bot.getKeyboard().typeKeyEvent((char)charCode, KeyEvent.KEY_PRESSED);
			Thread.sleep(50);
			bot.getKeyboard().typeKeyEvent((char)charCode, KeyEvent.KEY_RELEASED);			
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

	public BotCanvas getBotCanvas() {
		try {
			for (Method next : Bot.class.getMethods()) {
				next.setAccessible(true);
				if (next.getReturnType().equals(BotCanvas.class))
					return (BotCanvas) next.invoke(client.getBot());
			}
		} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
			e.printStackTrace();
		}
		return null;
	}
}

Main.java

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Box;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JToolBar;
import javax.swing.ListSelectionModel;


public abstract class View extends JFrame implements ActionListener {

	private static final long serialVersionUID = 1L;
	
	private DefaultListModel<String> model;
	private JList<String> lstText;
	private JButton btnAdd, btnRemove, btnSubmit;
	private JToolBar tlbButtonBar;
	
	private Object lock = new Object();
	
	public View() {
		
		initComponents();
		
	}
	
	public abstract void submitRequest(String text);
	
	@Override
	public void actionPerformed(ActionEvent e) {
		
		final String command = e.getActionCommand();
		
		if (command == null || command.isEmpty())
			return;

		final String selectedText = lstText.getSelectedValue();
		
		switch (command) {
		case "ADD":
			final String input = JOptionPane.showInputDialog(this, "Enter in text to add");
			if (input != null && !input.isEmpty() && !model.contains(input))
				model.addElement(input);
			else
				JOptionPane.showMessageDialog(this, "No valid text selected!");
			break;
		case "REMOVE":
			if (selectedText != null && !selectedText.isEmpty())
				model.removeElement(selectedText);
			else
				JOptionPane.showMessageDialog(this, "Select the text to remove!");
			break;
		case "SUBMIT":
			if (selectedText != null && !selectedText.isEmpty())
				submitRequest(selectedText);
			else
				JOptionPane.showMessageDialog(this, "Select the text to enter!");
			break;
		}

	}
	
	public void initComponents() {
		
		{ // Components
			
			{ // List
				model = new DefaultListModel<>();
				lstText = new JList<>(model);
				lstText.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
			}
			
			{ // Buttons
				btnAdd = new JButton("Add text");
				btnRemove = new JButton("Remove text");
				btnSubmit = new JButton("Submit text");
				
				btnAdd.setActionCommand("ADD");
				btnRemove.setActionCommand("REMOVE");
				btnSubmit.setActionCommand("SUBMIT");
				
				btnAdd.addActionListener(this);
				btnRemove.addActionListener(this);
				btnSubmit.addActionListener(this);
				
			}
			
			{ // Tool bar
				tlbButtonBar = new JToolBar();
				tlbButtonBar.setFloatable(false);
				tlbButtonBar.setRollover(true);
				
				tlbButtonBar.add(btnAdd);
				tlbButtonBar.add(btnRemove);
				tlbButtonBar.add(Box.createGlue());
				tlbButtonBar.add(btnSubmit);
			}
			
		}
		
		{ // This
			setSize(450, 335);
			setLayout(new BorderLayout());
			setTitle("Easy Keyboard Input");
			setAlwaysOnTop(true);
			setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
			setLocationRelativeTo(getOwner());
			setVisible(true);
		}
		
		{ // This (ADD)
			add(new JScrollPane(lstText,
					JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
					JScrollPane.HORIZONTAL_SCROLLBAR_NEVER),
					BorderLayout.CENTER);
			add(tlbButtonBar, BorderLayout.SOUTH);
		}
		
	}
	
	public boolean addText(String... arg0) {
		if (arg0 != null && arg0.length > 0) {
			for (String next : arg0)
				if (next != null && !next.isEmpty() && !model.contains(next))
					model.addElement(next);
			return true;
		}
		return false;
	}
	
	public void moveSelectionUp() {
		synchronized (lock) {
			int curSelect = lstText.getSelectedIndex();
			lstText.setSelectedIndex(curSelect <= 0 ? model.getSize() - 1 : curSelect - 1);
		}
	}

	public void moveSelectionDown() {
		synchronized (lock) {
			int curSelect = lstText.getSelectedIndex(), max = model.getSize();
			lstText.setSelectedIndex(curSelect >= max - 1 ? 0 : curSelect + 1);
		}
	}
	
	public void forceSubmit() {
		EventQueue.invokeLater(new Runnable() {			
			@Override
			public void run() {
				View.this.actionPerformed(new ActionEvent(btnSubmit, 0, btnSubmit.getActionCommand()));
			}
		});
	}
}

View.java

 

14djadi.png

 

 

Dropbox download!

 

Just an old script I made to spam my forum thread on RuneScape. And no I won't link you to it! smile.png

 

St00f:

  • TAB submits text to type in game.
  • Use mouse wheel to navigate the texts in the list. (Can do this while focused on the bot client.)
  • Add text, remove text, and submit text - all buttons on the GUI

 

Have fun!

 

[Posted this in snippets on purpose. Mods, feel free to add script to SDN - I don't really care.]

 

FORGOT TO MENTION: The built in #sendText method was really fucking slow, so I made mah own! :)

Edited by liverare
  • Like 1
Link to comment
Share on other sites

can you use this while using another script? im using a cooking bot and want to use this while it cook

 

Sure, but first you'd have to remove the whole onExit code block to prevent the exiting of the script to remove the listeners and prevent the disposal of the GUI.

	@Override
	public void onExit() throws InterruptedException {
		EventQueue.invokeLater(new Runnable() {
			@Override
			public void run() {
				view.dispose();
				
				if (bc != null) {
					bc.removeMouseWheelListener(Main.this);
					bc.removeKeyListener(Main.this);
				}
			}
		});
	}

EDIT: However I'd advise against it. If the cooking script uses the keyboard, there might be keyboard input conflict. :/

Edited by liverare
Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...