Arduino PRO MICRO macro keyboard, mouse


Warning

This posting is for educational purposes only. Never abuse please

 

Hello this is lifeonroom

Today, I’ll create a macro Keyboard, Mouse with serial data that came with Arduino Pro Micro.

The preparation is simple. The only Arduino Pro Micro

The Arduino Pro Micro is a ATMEGA32U4-compatible board that allows you to program on the chip. So all serial HID is possible! Just 5V version and 3.3 V are present. In the case of the 5V version, it works at 16MHz, and if you see the oscillator in the part of 5V, it will look like 16, and if it is 3.3 V, the number will be 8. 

Arduino Pro Micro 5V and 3.3 V version

The caveat is that if you choose not good on the board in the Arduino IDE and upload the sketch you can become a brick…. In my case, it was a 3.3 V boot loader Lily Pad Arduino Usb, which I uploaded to the Arduino/Genuino Micro and became a brick immediately. See the URL below for more information on how to recover the Hangul. Simply put the RST and GND twice and inject an empty sketch within 8 seconds.

Https://learn.sparkfun.com/tutorials/pro-micro–fio-v3-hookup-guide/troubleshooting-and-faq

You should be familiar with what board you are using and be careful when uploading sketches.  The Pro Micro is a good brick if you’re not good 

 

In my case, I worked with the 5V version, and when I connect the Arduino, I recognize it as Leonardo as shown below. Once this is recognized, it’s normal to work. 

1. Arduino code 

So let’s first squeeze the Arduino code. 

/*
/mMo/12,12|
/mlC/p|
/mlC/r|
/mKey/hello|
*/
#include "Mouse.h"
#include "Keyboard.h"
#include "HID.h"
int x,y = -1;
void setup() {
  Serial.begin(9600);
  // Sends a clean report to the host. This is important on any Arduino type.
  Keyboard.begin();
  Mouse.begin();
}
void loop() {
  if (Serial.available())
  {
    String mouseString = Serial.readStringUntil('|');
    mouseString.trim();
    if (mouseString.charAt(0) == '/')
    {
      // send like this - "/mMo/5,5"
      if (mouseString.substring(1, 4).equals(F("mMo")) == true)
      {
            //-------------------------------   
            int slashIdx = mouseString.indexOf('/', 1);
            if (slashIdx > 0)
            {
              int commaIdx = mouseString.indexOf(',', slashIdx + 1);
              if (commaIdx > 0)
              {
                x = mouseString.substring(slashIdx + 1, commaIdx).toInt();
                y = mouseString.substring(commaIdx + 1).toInt();
                
              }
              else
              {
                x = mouseString.substring(slashIdx + 1).toInt();
                y = 0;
              }
              Mouse.move(x, y, 0);
              //Serial.print(x);Serial.print(':');Serial.println(y);
            }
            //-------------------------------         
            Serial.println(F("move"));
      }
      else if (mouseString.substring(1, 4).equals(F("mlC")) == true)
      {
            //-------------------------------   
            int slashIdx = mouseString.indexOf('/', 1);
            if (slashIdx > 0)
            {
              char action = mouseString.substring(slashIdx + 1)[0];
              if ((action == 'p') && (!Mouse.isPressed(MOUSE_LEFT)))
              {
                Mouse.press(MOUSE_LEFT);
                Serial.println(F("Pre"));
              }
              else if ((action == 'r') && (Mouse.isPressed(MOUSE_LEFT)))
              {
                Mouse.release(MOUSE_LEFT);
                Serial.println(F("Rls"));
              }
            }
            else
            {
              Mouse.press();
              delay(400);
              Mouse.release();
              Serial.println(F("Click"));
            }
            //-------------------------------         
      }
      else if (mouseString.substring(1, 4).equals(F("mrC")) == true)
      {
            //-------------------------------   
            int slashIdx = mouseString.indexOf('/', 1);
            if (slashIdx > 0)
            {
              char action = mouseString.substring(slashIdx + 1)[0];
              if ((action == 'p') && (!Mouse.isPressed(MOUSE_RIGHT)))
              {
                Mouse.press(MOUSE_RIGHT);
                Serial.println(F("Pre"));
              }
              else if ((action == 'r') && (Mouse.isPressed(MOUSE_RIGHT)))
              {
                Mouse.release(MOUSE_RIGHT);
                Serial.println(F("Rls"));
              }
            }
            else
            {
              Mouse.press(MOUSE_RIGHT);
              delay(400);
              Mouse.release(MOUSE_RIGHT);
              Serial.println(F("Click"));
            }
      }
      else if (mouseString.substring(1, 5).equals(F("mKey")) == true)
      {
            //-------------------------------   
            int slashIdx = mouseString.indexOf('/', 1);
            if (slashIdx > 0)
            {
              
              Keyboard.print(mouseString.substring(slashIdx + 1));
            }
            //-------------------------------         
            
            Serial.println(F("Key"));
      }      
      
    }
  }
}

 

Below is an example of the command. Supports mouse move, left click, right click, Keyboard type. 

  • /MMo/12, 12 | -Move the mouse 12, 12, as far as the current position
  • /MlC/p | -Click the current coordinate mouse
  • /MlC/r | -Current Coordinate mouse
  • /Key/hello | -Enter Keyboard as the Hello 

Upload the sketch with the settings below. (5V 16MHz in use)

  • Board: Arduino/Genuino Micro
  • Port: COM21 (Arduino/Genuino Micro)

After uploading, open the serial monitor and try the above example!! If the command succeeds, it will Return a String as a Serial depending on the command type. Heh

2. Python Code 

If you press the keyboard ‘ F4 ‘ with the Python Code in the window, we’ll show an example that prints “Hello” after 3 seconds. First I’ll install the library below and go 

Pip Install Pyserial
Pip Install keyboard
Pip Install mouse

 

When you are done installing, create a file named serialFunc.py and enter the code below. 

import serial
import time
# /mMo/12,12|
# /mlC|
# /mlC/p|
# /mlC/r|
# /mrC|
# /mrC/p|
# /mrC/r|
# /mKey/keyinput|
class ExternalHID:
    ser = 0
    def __init__(self, comport):
        try:
            self.ser = serial.Serial(comport, 9600, timeout=1)
        except Exception as e:
            print(str(e))
    def disconnectSerial(self):
        self.ser.close()
    def checkSerial(self):
        if self.ser == 0:
            print('Serial Not available')
            return False
        else:
            return True
    def mouseMove(self, x, y):
        if not self.checkSerial():
            return False
        moveCommand = '/mMo/%d,%d|' % (x, y)
        self.ser.write(str.encode(moveCommand))
        rsp = self.ser.readline()
        if rsp.strip() == b'move':
            return True
        else:
            return False
    def mouseClick(self, button):
        if not self.checkSerial():
            return False
        if button == 'left':
            moveCommand = '/m%sC|' % ('l')
        elif button == 'right':
            moveCommand = '/m%sC|' % ('r')
        else:
            return False
        self.ser.write(str.encode(moveCommand))
        rsp = self.ser.readline()
        if rsp.strip() == b'Click':
            print('HID Click Success')
            return True
        else:
            return False
    def keyboardInput(self, keyinput):
        if not self.checkSerial():
            return False
        moveCommand = '/mKey/%s|' % keyinput
        self.ser.write(str.encode(moveCommand))
        rsp = self.ser.readline()
        if rsp.strip() == b'Key':
            return True
        else:
            return False
    def mousePress(self, button):
        if not self.checkSerial():
            return False
        print(str(button))
        if button == 'left':
            moveCommand = '/m%sC/p|' % ('l')
        elif button == 'right':
            moveCommand = '/m%sC/p|' % ('r')
        else:
            return False
        self.ser.write(str.encode(moveCommand))
        rsp = self.ser.readline()
        if rsp.strip() == b'Pre':
            return True
        else:
            return False
    def mouseRelease(self, button):
        if not self.checkSerial():
            return False
        print(str(button))
        if button == 'left':
            moveCommand = '/m%sC/r|' % ('l')
        elif button == 'right':
            moveCommand = '/m%sC/r|' % ('r')
        else:
            return False
        self.ser.write(str.encode(moveCommand))
        rsp = self.ser.readline()
        if rsp.strip() == b'Rls':
            return True
        else:
            return False
if __name__ == "__main__":
    import keyboard as key
    import time
    ser = ExternalHID('COM21')
    def printHello():
        state = False
        returnList = []
        while True:
            val = key.is_pressed('F4')
            if state != val:
                if val == True:
                    time.sleep(3)
                    ser.keyboardInput('Hello')
                state = val
    while True:
        time.sleep(0.001)
        printHello()

 

 

This code serves as a Serial communication with the Keyboard defined by Arduino and the Mouse control string. We’ve created a variety of functions, so try it out!

Once you’ve entered the code, run it in Pycharm or CMD, place your mouse in Notepad and press ‘ F4 ‘! After about 3 seconds, the word Hello will be written in Notepad. 

If you come here to see the Roah Fishing Macro series, what is this? Are you going to write Key.write (‘ Hello ‘) instead of just ser.keyboardInput (‘ Hello ‘)? A very reasonable statement called Anyplace. 

But the latter is an S/W method. When applied to the game, it is quite often not eaten. In particular, it is often not necessary to press certain buttons on the game in a mouse event. (Of course, my knowledge may be short… Very cheap 

This post is capable of hardware mechs and the exploits are banned. ..

Today’s post is up here. The staff were very friendly and accommodating.

 

You may also like...