2xRaspberry Pi nRF24L01 not connecting - python

I am new to Stackoverflow. I have searched for answer, but didn't find anything.
I have two Raspberry Pi 2B+, each with nRF24l01 connected. I found few libraries to make this connect, only one give any results, but not connections. This one: Github BLavery
I write script to send and to recv:
send.py:
import RPi.GPIO as GPIO
from lib_nrf24 import NRF24
import time
import spidev
GPIO.setmode(GPIO.BCM)
pipes = [[0xe7, 0xe7, 0xe7, 0xe7, 0xe7], [0xc2, 0xc2, 0xc2, 0xc2, 0xc2]]
radio = NRF24(GPIO, spidev.SpiDev())
radio.begin(0, 17)
radio.setPayloadSize(32)
radio.setChannel(0x60)
radio.setDataRate(NRF24.BR_2MBPS)
radio.setPALevel(NRF24.PA_MIN)
radio.setAutoAck(True)
radio.enableDynamicPayloads()
radio.enableAckPayload()
radio.openWritingPipe(pipes[1])
radio.printDetails()
while True:
message = list("Hello World")
radio.write(message)
print("We sent the message of {}".format(message))
# Check if it returned a ackPL
if radio.isAckPayloadAvailable():
returnedPL = []
radio.read(returnedPL, radio.getDynamicPayloadSize())
print("Our returned payload was {}".format(returnedPL))
else:
print("No payload received")
time.sleep(1)
recv.py:
import RPi.GPIO as GPIO
from lib_nrf24 import NRF24
import time
import spidev
GPIO.setmode(GPIO.BCM)
pipes = [[0xe7, 0xe7, 0xe7, 0xe7, 0xe7], [0xc2, 0xc2, 0xc2, 0xc2, 0xc2]]
radio = NRF24(GPIO, spidev.SpiDev())
radio.begin(0, 17)
radio.setPayloadSize(32)
radio.setChannel(0x60)
radio.setDataRate(NRF24.BR_2MBPS)
radio.setPAlevel(NRF24.PA_MIN)
radio.setAutoAck(True)
radio.enableDynamicPayloads()
radio.enableAckPayload()
radio.openReadingPipe(1, pipes[1])
radio.printDetails()
radio.startListening()
while True:
ackPL = [1]
while not radio.available (0):
time.sleep(1/100)
receivedMessage = []
radio.read(receivedMessage, radio.getDynamicPayloadSize())
print("Received: {}".format(receivedMessage))
print("Translating the receivedMessage into unicode characters...")
string = ""
for n in receivedMessage:
# Decode into standard i=unicode set
if (n >=32 and n <= 126):
string += chr(n)
print(string)
radio.writeAckPayload(1, ackPL, len(ackPL))
print("Loaded payload reply of {}".format(ackPL))
Everything seems to be alright, below are code returned by both scripts:
send:
STATUS = 0x03 RX_DR=0 TX_DS=0 MAX_RT=0 RX_P_NO=1 TX_FULL=1
RX_ADDR_P0-1 =
0xf8f8f8f8f8 0xf8f8f8f8f8
RX_ADDR_P2-5 =
0xf8
0xf9
0xf9
0xf9
TX_ADDR =
0xf8f8f8f8f8
RX_PW_P0-6 =
0x0c
0x00
0x00
0x00
0x00
0x00
EN_AA =
0x0f
EN_RXADDR =
0x00
RF_CH =
0x1c
RF_SETUP =
0x00
CONFIG =
0x03
DYNPD/FEATURE =
0x03
0x01
Data Rate = 1MBPS
Model = nRF24L01
CRC Length = Disabled
PA Power = PA_MIN
We sent the message of ['H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd']
No payload received
recv.py:
STATUS = 0x03 RX_DR=0 TX_DS=0 MAX_RT=0 RX_P_NO=1 TX_FULL=1
RX_ADDR_P0-1 =
0xf8f8f8f8f8 0xf8f8f8f8f8
RX_ADDR_P2-5 =
0xf8
0xf9
0xf9
0xf9
TX_ADDR =
0xf8f8f8f8f8
RX_PW_P0-6 =
0x0c
0x0c
0x00
0x00
0x00
0x00
EN_AA =
0x0f
EN_RXADDR =
0x00
RF_CH =
0x1c
RF_SETUP =
0x00
CONFIG =
0x03
DYNPD/FEATURE =
0x03
0x01
Data Rate = 1MBPS
Model = nRF24L01
CRC Length = Disabled
PA Power = PA_MIN
Received: []
Translating the receivedMessage into unicode characters...
Loaded payload reply of [1]
I don't really understand why it won't connect one to other,
Both have the same wiring:
nRF24L01-Raspberry Pi (Pin#)
GND - GND (6)
VCC - 3,3V (1)
CE - GPIO17 (11)
CSN - GPIO08(24)
SCK - GPIO11 (23)
MOSI - GPIO10 (19)
MISO - GPIO25 (22)
IRQ - unconnected
I need to send information from one RPi to second to control engine via PWM.
Can i ask for help

Related

write 8 bites to RS-485 using modbus_tk

Ethernet to RS-485 сonverter is connected to the computer and then I have 3 RS-485 controllers connected. I am writing registers to controllers using pymodbus. How can I do the same with modbus_tk?
from pymodbus.payload import BinaryPayloadBuilder
from pymodbus.utilities import computeCRC
Defaults.Timeout = 0.02
client = ModbusClient('192.168.0.191', port=9761)
client.connect()
builder = BinaryPayloadBuilder()
data = [0x00, 0x06, 0x00, 0x01, 0x00, 0xFF]
builder.add_8bit_uint(data[0])
builder.add_8bit_uint(data[1])
builder.add_8bit_uint(data[2])
builder.add_8bit_uint(data[3])
builder.add_8bit_uint(data[4])
builder.add_8bit_uint(data[5])
crc = computeCRC(data)
builder.add_8bit_uint(crc >> 8)
builder.add_8bit_uint(crc & 0x00FF)
payload = builder.build()
address = 0
client.write_registers(address, payload, skip_encode=True, unit=1)

Send APDU Commands in python (RC522, RaspberryPi)

I build up some kind of RFID-Reader with an Raspberry Pi 3 and an RC522 using this code as a start: https://github.com/mxgxw/MFRC522-python.
Unfortunately I'm not able to get a APDU-Response from my card after sending any APDU-Command.
My current workflow is more or less the ISO 14443-4:
Search for cards
receive ATQA
Perform anticollision and select card
Determine whether card is supporting ISO-14443-4 protocol or not
Send RATS and receive ATS
Perform an PPS-Request [Until here everything is fine and I get responses.]
Send an APDU-Command [btw. command works fine with my smartphone and an APP]
#Scan for cards
(status,TagType) = MIFAREReader.MFRC522_Request(MIFAREReader.PICC_REQIDL)
#If a card is found
if status == MIFAREReader.MI_OK:
print "Card detected"
TagType = (TagType >> 6) | 0 #(Länge der UID errechnen
print "Tagtype: %s" % (TagType)
# START ANTI-COLLISION AS OF ISO 14443
if TagType == 0:
print "UID length is single -- starting ANTI-COLLISION CL1"
(status,uid) = MIFAREReader.MFRC522_Anticoll()
if status == MIFAREReader.MI_OK:
print "Card read UID: %s,%s,%s,%s" % (uid[0], uid[1], uid[2], uid[3])
elif TagType == 1: # Ignore this part as there is no functionality by now
print "CARD IS NOT SUPPORTED YET"
(status,uid) = MIFAREReader.MFRC522_Anticoll()
elif TagType == 2:
print "CARD IS NOT SUPPORTED YET"
(status,uid) = MIFAREReader.MFRC522_Anticoll()
elif TagType == 3:
print "CARD IS NOT SUPPORTED YET"
(status,uid) = MIFAREReader.MFRC522_Anticoll()
else:
print "An unexpected error occured: UID length is not covered by ISO-14443."
(size, APDU) = MIFAREReader.MFRC522_SelectTag(uid)
print "Card selected"
print "ISO 14443-4 Compatibility: %s" % (APDU)
if APDU == 1: # Card supports ISO 14443-4 protocol [Problem could be here]
#SEND RATS
print "Sening RATS"
RATS_Sequenz = [0xE0, 0x50]
CRC_RATS = []
CRC_RATS = MIFAREReader.CalulateCRC(RATS_Sequenz)
RATS_Sequenz.append(CRC_RATS[0])
RATS_Sequenz.append(CRC_RATS[1])
(stat, res, leng) = MIFAREReader.MFRC522_ToCard(MIFAREReader.PCD_TRANSCEIVE, RATS_Sequenz)
# Muss: Auswertung des ATS
# PPS
print "Initialisiere PPS-Kommando"
PPSS_Sequenz = [0xD0, 0x11, 0x00]
CRC_PPSS = []
CRC_PPSS = MIFAREReader.CalulateCRC(PPSS_Sequenz)
PPSS_Sequenz.append(CRC_PPSS[0])
PPSS_Sequenz.append(CRC_PPSS[1])
(stat, res, leng) = MIFAREReader.MFRC522_ToCard(MIFAREReader.PCD_TRANSCEIVE, PPSS_Sequenz)
#PCB = [0x02]
#PCB.append(PCB[0])
#PCB.append(PCB[1])
#(stat, res, leng) = MIFAREReader.MFRC522_ToCard(MIFAREReader.PCD_TRANSCEIVE, PCB)
command = [0xFF, 0x41, 0x00, 0x00, 0x00] # NOT THE COMMAND!
Header = [0x00, 0x22, 0xC1, 0xA4, 0x12, 0x80, 0x0A, 0x04, 0x00, 0x7F, 0x00, 0x07, 0x02, 0x02, 0x04, 0x02, 0x02, 0x83, 0x01, 0x02, 0x84, 0x01, 0x0D] #Command to be sent
pOut = MIFAREReader.CalulateCRC(Header)
Header.append(pOut[0])
Header.append(pOut[1])
(stat, res, leng) = MIFAREReader.MFRC522_ToCard(MIFAREReader.PCD_TRANSCEIVE, (Header))
time.sleep(1) # Leftover from here / another non working try
Body = [0x80, 0x0A, 0x04, 0x00, 0x7F, 0x00, 0x07, 0x02, 0x02, 0x04, 0x02, 0x02, 0x83, 0x01, 0x02, 0x84, 0x01, 0x0D]
#pOut = MIFAREReader.CalulateCRC(Body)
#Body.append(pOut[0])
#Body.append(pOut[1])
#(stat, res, leng) = MIFAREReader.MFRC522_ToCard(MIFAREReader.PCD_TRANSCEIVE, Body)
elif APDU == 0:
#HALT
command = [0x50]
pOut = MIFAREReader.CalulateCRC(command)
command.append(pOut[0])
command.append(pOut[1])
(stat, res, leng) = MIFAREReader.MFRC522_ToCard(MIFAREReader.PCD_TRANSCEIVE, command)
#WOP
This is the send function as found in the 'library':
def MFRC522_ToCard(self,command,sendData):
backData = []
backLen = 0
status = self.MI_ERR
irqEn = 0x00
waitIRq = 0x00
lastBits = None
n = 0
i = 0
if command == self.PCD_AUTHENT:
irqEn = 0x12
waitIRq = 0x10
if command == self.PCD_TRANSCEIVE:
irqEn = 0x77
waitIRq = 0x30
self.Write_MFRC522(self.CommIEnReg, irqEn|0x80)
self.ClearBitMask(self.CommIrqReg, 0x80)
self.SetBitMask(self.FIFOLevelReg, 0x80)
self.Write_MFRC522(self.CommandReg, self.PCD_IDLE);
while(i<len(sendData)):
self.Write_MFRC522(self.FIFODataReg, sendData[i])
i = i+1
self.Write_MFRC522(self.CommandReg, command)
if command == self.PCD_TRANSCEIVE:
self.SetBitMask(self.BitFramingReg, 0x80)
i = 2000
while True:
n = self.Read_MFRC522(self.CommIrqReg)
i = i - 1
if ~((i!=0) and ~(n&0x01) and ~(n&waitIRq)):
break
self.ClearBitMask(self.BitFramingReg, 0x80)
if i != 0:
if (self.Read_MFRC522(self.ErrorReg) & 0x1B)==0x00:
status = self.MI_OK
if n & irqEn & 0x01:
status = self.MI_NOTAGERR
if command == self.PCD_TRANSCEIVE:
n = self.Read_MFRC522(self.FIFOLevelReg)
lastBits = self.Read_MFRC522(self.ControlReg) & 0x07
if lastBits != 0:
backLen = (n-1)*8 + lastBits
else:
backLen = n*8
if n == 0:
n = 1
if n > self.MAX_LEN:
n = self.MAX_LEN
i = 0
while i<n:
backData.append(self.Read_MFRC522(self.FIFODataReg))
i = i + 1;
else:
status = self.MI_ERR
print"Status: %s, Data: %s, Length: %s" % (status, backData, backLen)
return (status,backData,backLen)
The command is send using the 'toCard' function of the code mentioned above with following structure:
APDU-Command + CRC-Bytes.
e.g. 0x00 [CLA], 0x22 [INS], 0xC1 [P1], 0xA4 [P2], 0x12 [Length], 0x80 [DATA, 0x0A, 0x04, 0x00, 0x7F, 0x00, 0x07, 0x02, 0x02, 0x04, 0x02, 0x02, 0x83, 0x01, 0x02, 0x84, 0x01, 0x0D, CRC1, CRC2
I also tried sending the Command with an additional PCB or in multiple parts (e.g. Header first then body).
I'm not sure if the structure of the APDU-Command is or if I forgot some part of the activation process as of ISO 14443. I'd be glad if anybody can help me.

hd44780 i2c lcd cursor print on the same place

I have a matrix keypad and a hd44780 lcd connected into raspberry pi.
The below code are in python. It will take 4 digit password from the user, but when the user enter the digit through the keypad, nothing was display on the lcd. I would like when the user enter a digit a * display on the lcd. I can only manage to get the * print in the same place,but not in 4 separate place.
Code
import RPi.GPIO as GPIO
import time
import lcd
import os
import subprocess
from subprocess import call
mylcd = lcd.lcd()
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
MATRIX = [['1', '2', '3', 'A'],
['4', '5', '6', 'B'],
['7', '8', '9', 'C'],
['*', '0', '#', 'D']]
ROW = [5,6,13,19]
COL = [26,16,20,21]
for j in range(4):
GPIO.setup(COL[j], GPIO.OUT)
GPIO.output(COL[j], 1)
for i in range(4):
GPIO.setup(ROW[i], GPIO.IN, pull_up_down=GPIO.PUD_UP)
password = "1234"
attempt = ""
try:
while (True):
mylcd.lcd_display_string("Enter pw",1)
for j in range(4):
GPIO.output(COL[j], 0)
for i in range(4):
if GPIO.input(ROW[i]) == 0:
mylcd.lcd_display_string("*",2,0)
time.sleep(0.01)
while (GPIO.input(ROW[i]) == 0):
pass
attempt += MATRIX[i][j]
if len(attempt) == len(password):
if attempt == password:
mylcd.lcd_clear()
print "Password OK"
mylcd.lcd_display_string("Password",1 ,4 )
mylcd.lcd_display_string("Ok!",2 ,6 )
time.sleep(2)
mylcd.lcd_clear()
os.chdir('/home/pi/nfc/py532lib')
call('./nfc.sh')
mylcd.lcd_clear()
else:
print(attempt)
print "Password incorrect"
mylcd.lcd_display_string("Password",1,4)
mylcd.lcd_display_string("Incorrect!",2,3)
time.sleep(2)
mylcd.lcd_clear()
attempt = ""
time.sleep(0.01)
GPIO.output(COL[j], 1)
except KeyboardInterrupt:
GPIO.cleanup()
LCD Library
# -*- coding: utf-8 -*-
# Original code found at:
# https://gist.github.com/DenisFromHR/cc863375a6e19dce359d
"""
Compiled, mashed and generally mutilated 2014-2015 by Denis Pleic
Made available under GNU GENERAL PUBLIC LICENSE
# Modified Python I2C library for Raspberry Pi
# as found on http://www.recantha.co.uk/blog/?p=4849
# Joined existing 'i2c_lib.py' and 'lcddriver.py' into a single library
# added bits and pieces from various sources
# By DenisFromHR (Denis Pleic)
# 2015-02-10, ver 0.1
"""
# i2c bus (0 -- original Pi, 1 -- Rev 2 Pi)
I2CBUS = 0
# LCD Address
ADDRESS = 0x27
import smbus
from time import sleep
class i2c_device:
def __init__(self, addr, port=I2CBUS):
self.addr = addr
self.bus = smbus.SMBus(port)
# Write a single command
def write_cmd(self, cmd):
self.bus.write_byte(self.addr, cmd)
sleep(0.0001)
# Write a command and argument
def write_cmd_arg(self, cmd, data):
self.bus.write_byte_data(self.addr, cmd, data)
sleep(0.0001)
# Write a block of data
def write_block_data(self, cmd, data):
self.bus.write_block_data(self.addr, cmd, data)
sleep(0.0001)
# Read a single byte
def read(self):
return self.bus.read_byte(self.addr)
# Read
def read_data(self, cmd):
return self.bus.read_byte_data(self.addr, cmd)
# Read a block of data
def read_block_data(self, cmd):
return self.bus.read_block_data(self.addr, cmd)
# commands
LCD_CLEARDISPLAY = 0x01
LCD_RETURNHOME = 0x02
LCD_ENTRYMODESET = 0x04
LCD_DISPLAYCONTROL = 0x08
LCD_CURSORSHIFT = 0x10
LCD_FUNCTIONSET = 0x20
LCD_SETCGRAMADDR = 0x40
LCD_SETDDRAMADDR = 0x80
# flags for display entry mode
LCD_ENTRYRIGHT = 0x00
LCD_ENTRYLEFT = 0x02
LCD_ENTRYSHIFTINCREMENT = 0x01
LCD_ENTRYSHIFTDECREMENT = 0x00
# flags for display on/off control
LCD_DISPLAYON = 0x04
LCD_DISPLAYOFF = 0x00
LCD_CURSORON = 0x02
LCD_CURSOROFF = 0x00
LCD_BLINKON = 0x01
LCD_BLINKOFF = 0x00
# flags for display/cursor shift
LCD_DISPLAYMOVE = 0x08
LCD_CURSORMOVE = 0x00
LCD_MOVERIGHT = 0x04
LCD_MOVELEFT = 0x00
# flags for function set
LCD_8BITMODE = 0x10
LCD_4BITMODE = 0x00
LCD_2LINE = 0x08
LCD_1LINE = 0x00
LCD_5x10DOTS = 0x04
LCD_5x8DOTS = 0x00
# flags for backlight control
LCD_BACKLIGHT = 0x08
LCD_NOBACKLIGHT = 0x00
En = 0b00000100 # Enable bit
Rw = 0b00000010 # Read/Write bit
Rs = 0b00000001 # Register select bit
class lcd:
#initializes objects and lcd
def __init__(self):
self.lcd_device = i2c_device(ADDRESS)
self.lcd_write(0x03)
self.lcd_write(0x03)
self.lcd_write(0x03)
self.lcd_write(0x02)
self.lcd_write(LCD_FUNCTIONSET | LCD_2LINE | LCD_5x8DOTS | LCD_4BITMODE)
self.lcd_write(LCD_DISPLAYCONTROL | LCD_DISPLAYON)
self.lcd_write(LCD_CLEARDISPLAY)
self.lcd_write(LCD_ENTRYMODESET | LCD_ENTRYLEFT)
sleep(0.2)
# clocks EN to latch command
def lcd_strobe(self, data):
self.lcd_device.write_cmd(data | En | LCD_BACKLIGHT)
sleep(.0005)
self.lcd_device.write_cmd(((data & ~En) | LCD_BACKLIGHT))
sleep(.0001)
def lcd_write_four_bits(self, data):
self.lcd_device.write_cmd(data | LCD_BACKLIGHT)
self.lcd_strobe(data)
# write a command to lcd
def lcd_write(self, cmd, mode=0):
self.lcd_write_four_bits(mode | (cmd & 0xF0))
self.lcd_write_four_bits(mode | ((cmd << 4) & 0xF0))
# write a character to lcd (or character rom) 0x09: backlight | RS=DR<
# works!
def lcd_write_char(self, charvalue, mode=1):
self.lcd_write_four_bits(mode | (charvalue & 0xF0))
self.lcd_write_four_bits(mode | ((charvalue << 4) & 0xF0))
# put string function with optional char positioning
def lcd_display_string(self, string, line=1, pos=0):
if line == 1:
pos_new = pos
elif line == 2:
pos_new = 0x40 + pos
elif line == 3:
pos_new = 0x14 + pos
elif line == 4:
pos_new = 0x54 + pos
self.lcd_write(0x80 + pos_new)
for char in string:
self.lcd_write(ord(char), Rs)
# clear lcd and set to home
def lcd_clear(self):
self.lcd_write(LCD_CLEARDISPLAY)
self.lcd_write(LCD_RETURNHOME)
# define backlight on/off (lcd.backlight(1); off= lcd.backlight(0)
def backlight(self, state): # for state, 1 = on, 0 = off
if state == 1:
self.lcd_device.write_cmd(LCD_BACKLIGHT)
elif state == 0:
self.lcd_device.write_cmd(LCD_NOBACKLIGHT)
# add custom characters (0 - 7)
def lcd_load_custom_chars(self, fontdata):
self.lcd_write(0x40);
for char in fontdata:
for line in char:
self.lcd_write_char(line)
Your code may be not correct. For each output you test all inputs:
for i in range(4):
if GPIO.input(ROW[i]) == 0:
Check if for sure exactly one proper input is active. If not, you have to rewrite it.
Moreover, the number of * should depend on external counter j, not i (my mistake in the comment, sorry):
mylcd.lcd_display_string((j+1) * "*", 2, 0)

Python3 - seek and write an exe address

I have a file (original.exe) which I edit using OllyDbg but I want to learn how to do this with Python.
In OllyDbg I go to Address:
0045B82C
Then I would change:
JNZ SHORT 0045B89F
To:
JE SHORT 0045B90F
So in python using readtest.py:
offset = 0x0045B82C
with open('original.exe', 'r+b') as victim:
victim.seek(offset)
line = victim.readline()
print(line)
I get:
b'Wf93\x8b\xe9\x89t$\x10u\x05\xe8\x83\'\x00\x00f\x81{\x02\xd3\x07sP\xa1\xe4\xf2\x9c\x00\x8b\x15\xa8#\xae\x00\x8d\x0c\xc0\xc1\xe1\x04\x03\xc8\x8d\x84J\x80\x00\x00\x00\x8b\x8cJ\x80\x00\x00\x00;\xcet+\x8b\t\x89\r\xa45\xa8\x00\x890\xa1\xe4\xf2\x9c\x00\x8d\x14\xc0\xc1\xe2\x04\x03\xd0\xa1\xa8#\xae\x00\x8d\x0cPQ\xe8A\xfb\xcd\xff\x83\xc4\x04\x89pij\xffh\xd3\x07\x00\x00j\x06j\x01\x8dL$,\xe8\xc7\xe6\xce\xff\x8b\x08\x8bP\x04\x89L$\x14f\x8bD$\x16f;C\x02\x89T$\x18\x0f\x85\xa4\x00\x00\x00f;\x0b\x0f\x85\x9b\x00\x00\x00\xa1\xa45\xa8\x00\x83\xf8\xff\x0f\x84\x8d\x00\x00\x00\x8b\x15\xb0#\xae\x00\x8d\x0c\x80\xc1\xe1\x03+\xc8\xa1\xe4\xf2\x9c\x00\x8d\x0cJ\x8d\x14\xc0\xc1\xe2\x04\x03\xd0\xa1\xa8#\xae\x00\x89\x8cP\x80\x00\x00\x00\xa1\xe4\xf2\x9c\x00\x8b\x15\xa8#\xae\x00\x8d\x0c\xc0\xc1\xe1\x04\x03\xc8\x8d4JV\xe8\xae\xfa\xcd\xff\x8b\x8e\x80\x00\x00\x00\x83\xc4\x04\x89Hi\xa1\xa45\xa8\x00\xb9\x90_\x01\x00\x8d\x14\x80\xc1\xe2\x03+\xd0\xa1\xb0#\xae\x00\x89LP<\xa1\xa45\xa8\x00\x8d\x14\x80\xc1\xe2\x03+\xd0\xa1\xb0#\xae\x00\x89LP#\xc7\x05\xa45\xa8\x00\xff\xff\xff\xff\x0f\xbf\x03\x99\xb9\x1e\x00\x00\x00\xf7\xf9\x85\xd2\x0f\x85\xb2\x00\x00\x00\x8b}\x00\x85\xff\x0f\x84\xa7\x00\x00\x00\x8bE\x043\xf6\x85\xc0\x0f\x8e\x9a\x00\x00\x00\x8b\x13\x8bC\x04\x89T$\x14f\x8bT$\x16\x89D$\x183\xc0\x8d\x0c#\x8dD\x8f\x04f\x8bL\x8f\x06f;\xd1w\x15r\'f\x8b\\$\x143\xc9f;\x18\x0f\x9f\xc1\x8b\xc1\x85\xc0t\x14\x8bD$\x10\x8bM\x04#F\x89D$\x10\x0f\xbf\xc6;\xc1|\xc6\x8bD$\x10f\x85\xc0tC\x0f\xbf\xd0\x8bE\x04;\xd0}\x1d\x8d\x0cR+\xc2\xc1\xe1\x02H\x8d\x04#\x8d4\x87\x8b\xc1\xc1\xe9\x02\xf3\xa5\x8b\xc8\x83\xe1\x03\xf3\xa4\x8bE\x04\x8bM\x00+\xc2h\x80\xe3\x85\x00j\x0cPQ\x89E\x04\xe8\x08\x99\x0e\x00\x83\xc4\x10_^][\x83\xc4\x14\xc2\x04\x00\x90\x90\x90\x90\x90\x90\x90\x90\x81\xec\x04\x02\x00\x00SUVWh(\xf9\x98\x00h\xe05\xa8\x00\x8b\xf9h\xa0}\x98\x00\xe8\x80\x03\x0b\x00\x83\xc4\x08P\xe8\x97\xa0\x0e\x00\x8b\xf0\x83\xc4\x08\x85\xf6\x0f\x84"\x03\x00\x00V\x8do\x08j\x01j\x04U\xe8z\xa1\x0e\x00\x83\xc4\x10\x83\xf8\x01tv\x8dD$\x14\x8d\x8c$\x14\x01\x00\x00PQj\x00j\x00h\xb05\xa8\x00\xe8X\x92\x0e\x00\x83\xc4\x14\x8dT$\x14\x8d\x84$\x14\x01\x00\x00h\xf2\x00\x00\x00RP\xe8o\x16\x0b\x00Ph\xe8p\x98\x00h\xd0$\xae\x00\xe8\xce\x91\x0e\x00j\x00h\xd0$\xae\x00h\xe0p\x98\x00\xe8\xae\xc7\xd8\xffV\xc7\x054z\xb6\x00\x00\x00\x00\x00\xe8\x17\x9f\x0e\x00\x83\xc4(3\xc0_^][\x81\xc4\x04\x02\x00\x00\xc3V\x8d_\x04j\x01j\x04S\xe8\xee\xa0\x0e\x00\x83\xc4\x10\x83\xf8\x01t5\x8d\x8c$\x14\x01\x00\x00\x8dT$\x14QRj\x00j\x00h\xb05\xa8\x00\xe8\xcc\x91\x0e\x00\x83\xc4\x14\x8d\x84$\x14\x01\x00\x00\x8dL$\x14h\xf9\x00\x00\x00PQ\xe9o\xff\xff\xff\x8bE\x003\xed;\xc5tG\x8d\x14#\xc1\xe2\x02R\xe8\xe4\x92\x0e\x00\x83\xc4\x04;\xc5\x89\x07u2\x8d\x84$\x14\x01\x00\x00\x8dL$\x14PQUUh\xb05\xa8\x00\xe8{\x91\x0e\x00\x83\xc4\x14\x8d\x94$\x14\x01\x00\x00\x8dD$\x14h\x05\x01\x00\x00R\xe9\xc6\x01\x00\x00\x8b\x03;\xc5tH\x8b\x0fVPj\x0cQ\xe8O\xa0\x0e\x00\x8b\x0b\x83\xc4\x10;\xc1t3\x8d\x94$\x14\x01\x00\x00\x8dD$\x14RPUUh\xb05\xa8\x00\xe8.\x91\x0e\x00\x83\xc4\x14\x8d\x8c$\x14\x01\x00\x00\x8dT$\x14h\x13\x01\x00\x00QR\xe9y\x01\x00\x00V\x8do\x14j\x01j\x04U\xe8\x05\xa0\x0e\x00\x83\xc4\x10\x83\xf8\x01t4\x8d\x84$\x14\x01\x00\x00\x8dL$\x14PQj\x00j\x00h\xb05\xa8\x00\xe8\xe3\x90\x0e\x00\x83\xc4\x14\x8d\x94$\x14\x01\x00\x00\x8dD$\x14h\x1d\x01\x00\x00R\xe9\x86\xfe\xff\xffV\x8d_\x10j\x01j\x04S\xe8\xbb\x9f\x0e\x00\x83\xc4\x10\x83\xf8\x01t5\x8d\x8c$\x14\x01\x00\x00\x8dT$\x14QRj\x00j\x00h\xb05\xa8\x00\xe8\x99\x90\x0e\x00\x83\xc4\x14\x8d\x84$\x14\x01\x00\x00\x8dL$\x14h$\x01\x00\x00PQ\xe9<\xfe\xff\xffVj\x01\x8dW\x18j\x01R\xe8p\x9f\x0e\x00\x83\xc4\x10\x83\xf8\x01t4\x8d\x84$\x14\x01\x00\x00\x8dL$\x14PQj\x00j\x00h\xb05\xa8\x00\xe8N\x90\x0e\x00\x83\xc4\x14\x8d\x94$\x14\x01\x00\x00\x8dD$\x14h+\x01\x00\x00R\xe9\xf1\xfd\xff\xff\x8bE\x003\xed;\xc5tC\xc1\xe0\x04P\xe8j\x91\x0e\x00\x83\xc4\x04;\xc5\x89G\x0cu0\x8d\x8c$\x14\x01\x00\x00\x8dT$\x14QRUUh\xb05\xa8\x00\xe8\x00\x90\x0e\x00\x83\xc4\x14\x8d\x84$\x14\x01\x00\x00\x8dL$\x14h7\x01\x00\x00PQ\xebN\x8b\x03;\xc5\x0f\x84\xc9\x00\x00\x00\x8bW\x0cVPj\x10R\xe8\xd1\x9e\x0e\x00\x8b\x0b\x83\xc4\x10;\xc1to\x8d\x84$\x14\x01\x00\x00\x8dL$\x14PQUUh\xb05\xa8\x00\xe8\xb0\x8f\x0e\x00\x83\xc4\x14\x8d\x94$\x14\x01\x00\x00\x8dD$\x14hE\x01\x00\x00RP\xe8\xc7\x13\x0b\x00Ph\xe8p\x98\x00h\xd0$\xae\x00\xe8&\x8f\x0e\x00Uh\xd0$\xae\x00h\xe0p\x98\x00\xe8\x07\xc5\xd8\xffV\x89-4z\xb6\x00\xe8t\x9c\x0e\x00\x83\xc4(_^]3\xc0[\x81\xc4\x04\x02\x00\x00\xc3;\xcd\x89l$\x10~<3\xd2\x8bO\x0c\x8bD\x11\x0c\x8dL\x11\x0c\x83\xf8\xfft\x16\x8d,\x80\xc1\xe5\x03+\xe8\xa1\xb0#\xae\x00\x8d\x04h3\xed\x89\x01\xeb\x02\x89)\x8bD$\x10\x8b\x0b#\x83\xc2\x10;\xc1\x89D$\x10|\xc6\xa1\x10q\xdd\x00\x993\xc2+\xc2=)\xe0\x03\x00\x0f\x8c\x80\x00\x00\x00Vj\x01j\x04h\xa45\xa8\x00\xe8\xf1\x9d\x0e\x00\x83\xc4\x10\x83\xf8\x01ti\x8d\x8c$\x14\x01\x00\x00\x8dT$\x14QRUUh\xb05\xa8\x00\xe8\xd1\x8e\x0e\x00\x83\xc4\x14\x8d\x84$\x14\x01\x00\x00\x8dL$\x14h\\\x01\x00\x00PQ\xe8\xe8\x12\x0b\x00Ph\xe8p\x98\x00h\xd0$\xae\x00\xe8G\x8e\x0e\x00Uh\xd0$\xae\x00h\xe0p\x98\x00\xe8(\xc4\xd8\xff\x83\xc4$\x89-4z\xb6\x003\xc0_^][\x81\xc4\x04\x02\x00\x00\xc3V\xe8\x85\x9b\x0e\x00\x83\xc4\x04\xb8\x01\x00\x00\x00_^][\x81\xc4\x04\x02\x00\x00\xc3\x90\x90\x90\x90\x90\x90\x90\x90\x90\x81\xec\x04\x02\x00\x00SUVWh(\x7f\x9a\x00h\xe05\xa8\x00\x8b\xf1h\xa0}\x98\x00\xe8 \xff\n'
I then apply the changes using OllyDbg and re run readtest.py
b'Wf93\x8b\xe9\x89t$\x10u\x05\xe8\x83\'\x00\x00f\x81{\x02\xd3\x07sP\xa1\xe4\xf2\x9c\x00\x8b\x15\xa8#\xae\x00\x8d\x0c\xc0\xc1\xe1\x04\x03\xc8\x8d\x84J\x80\x00\x00\x00\x8b\x8cJ\x80\x00\x00\x00;\xcet+\x8b\t\x89\r\xa45\xa8\x00\x890\xa1\xe4\xf2\x9c\x00\x8d\x14\xc0\xc1\xe2\x04\x03\xd0\xa1\xa8#\xae\x00\x8d\x0cPQ\xe8A\xfb\xcd\xff\x83\xc4\x04\x89pij\xffh\xd3\x07\x00\x00j\x06j\x01\x8dL$,\xe8\xc7\xe6\xce\xff\x8b\x08\x8bP\x04\x89L$\x14f\x8bD$\x16f;C\x02\x89T$\x18\x0f\x85\xa4\x00\x00\x00f;\x0b\x0f\x85\x9b\x00\x00\x00\xa1\xa45\xa8\x00\x83\xf8\xff\x0f\x84\x8d\x00\x00\x00\x8b\x15\xb0#\xae\x00\x8d\x0c\x80\xc1\xe1\x03+\xc8\xa1\xe4\xf2\x9c\x00\x8d\x0cJ\x8d\x14\xc0\xc1\xe2\x04\x03\xd0\xa1\xa8#\xae\x00\x89\x8cP\x80\x00\x00\x00\xa1\xe4\xf2\x9c\x00\x8b\x15\xa8#\xae\x00\x8d\x0c\xc0\xc1\xe1\x04\x03\xc8\x8d4JV\xe8\xae\xfa\xcd\xff\x8b\x8e\x80\x00\x00\x00\x83\xc4\x04\x89Hi\xa1\xa45\xa8\x00\xb9\x90_\x01\x00\x8d\x14\x80\xc1\xe2\x03+\xd0\xa1\xb0#\xae\x00\x89LP<\xa1\xa45\xa8\x00\x8d\x14\x80\xc1\xe2\x03+\xd0\xa1\xb0#\xae\x00\x89LP#\xc7\x05\xa45\xa8\x00\xff\xff\xff\xff\x0f\xbf\x03\x99\xb9\x1e\x00\x00\x00\xf7\xf9\x85\xd2\x0f\x85\xb2\x00\x00\x00\x8b}\x00\x85\xff\x0f\x84\xa7\x00\x00\x00\x8bE\x043\xf6\x85\xc0\x0f\x8e\x9a\x00\x00\x00\x8b\x13\x8bC\x04\x89T$\x14f\x8bT$\x16\x89D$\x183\xc0\x8d\x0c#\x8dD\x8f\x04f\x8bL\x8f\x06f;\xd1w\x15r\'f\x8b\\$\x143\xc9f;\x18\x0f\x9f\xc1\x8b\xc1\x85\xc0t\x14\x8bD$\x10\x8bM\x04#F\x89D$\x10\x0f\xbf\xc6;\xc1|\xc6\x8bD$\x10f\x85\xc0tC\x0f\xbf\xd0\x8bE\x04;\xd0}\x1d\x8d\x0cR+\xc2\xc1\xe1\x02H\x8d\x04#\x8d4\x87\x8b\xc1\xc1\xe9\x02\xf3\xa5\x8b\xc8\x83\xe1\x03\xf3\xa4\x8bE\x04\x8bM\x00+\xc2h\x80\xe3\x85\x00j\x0cPQ\x89E\x04\xe8\x08\x99\x0e\x00\x83\xc4\x10_^][\x83\xc4\x14\xc2\x04\x00\x90\x90\x90\x90\x90\x90\x90\x90\x81\xec\x04\x02\x00\x00SUVWh(\xf9\x98\x00h\xe05\xa8\x00\x8b\xf9h\xa0}\x98\x00\xe8\x80\x03\x0b\x00\x83\xc4\x08P\xe8\x97\xa0\x0e\x00\x8b\xf0\x83\xc4\x08\x85\xf6\x0f\x84"\x03\x00\x00V\x8do\x08j\x01j\x04U\xe8z\xa1\x0e\x00\x83\xc4\x10\x83\xf8\x01tv\x8dD$\x14\x8d\x8c$\x14\x01\x00\x00PQj\x00j\x00h\xb05\xa8\x00\xe8X\x92\x0e\x00\x83\xc4\x14\x8dT$\x14\x8d\x84$\x14\x01\x00\x00h\xf2\x00\x00\x00RP\xe8o\x16\x0b\x00Ph\xe8p\x98\x00h\xd0$\xae\x00\xe8\xce\x91\x0e\x00j\x00h\xd0$\xae\x00h\xe0p\x98\x00\xe8\xae\xc7\xd8\xffV\xc7\x054z\xb6\x00\x00\x00\x00\x00\xe8\x17\x9f\x0e\x00\x83\xc4(3\xc0_^][\x81\xc4\x04\x02\x00\x00\xc3V\x8d_\x04j\x01j\x04S\xe8\xee\xa0\x0e\x00\x83\xc4\x10\x83\xf8\x01t5\x8d\x8c$\x14\x01\x00\x00\x8dT$\x14QRj\x00j\x00h\xb05\xa8\x00\xe8\xcc\x91\x0e\x00\x83\xc4\x14\x8d\x84$\x14\x01\x00\x00\x8dL$\x14h\xf9\x00\x00\x00PQ\xe9o\xff\xff\xff\x8bE\x003\xed;\xc5tG\x8d\x14#\xc1\xe2\x02R\xe8\xe4\x92\x0e\x00\x83\xc4\x04;\xc5\x89\x07u2\x8d\x84$\x14\x01\x00\x00\x8dL$\x14PQUUh\xb05\xa8\x00\xe8{\x91\x0e\x00\x83\xc4\x14\x8d\x94$\x14\x01\x00\x00\x8dD$\x14h\x05\x01\x00\x00R\xe9\xc6\x01\x00\x00\x8b\x03;\xc5tH\x8b\x0fVPj\x0cQ\xe8O\xa0\x0e\x00\x8b\x0b\x83\xc4\x10;\xc1t3\x8d\x94$\x14\x01\x00\x00\x8dD$\x14RPUUh\xb05\xa8\x00\xe8.\x91\x0e\x00\x83\xc4\x14\x8d\x8c$\x14\x01\x00\x00\x8dT$\x14h\x13\x01\x00\x00QR\xe9y\x01\x00\x00V\x8do\x14j\x01j\x04U\xe8\x05\xa0\x0e\x00\x83\xc4\x10\x83\xf8\x01t4\x8d\x84$\x14\x01\x00\x00\x8dL$\x14PQj\x00j\x00h\xb05\xa8\x00\xe8\xe3\x90\x0e\x00\x83\xc4\x14\x8d\x94$\x14\x01\x00\x00\x8dD$\x14h\x1d\x01\x00\x00R\xe9\x86\xfe\xff\xffV\x8d_\x10j\x01j\x04S\xe8\xbb\x9f\x0e\x00\x83\xc4\x10\x83\xf8\x01t5\x8d\x8c$\x14\x01\x00\x00\x8dT$\x14QRj\x00j\x00h\xb05\xa8\x00\xe8\x99\x90\x0e\x00\x83\xc4\x14\x8d\x84$\x14\x01\x00\x00\x8dL$\x14h$\x01\x00\x00PQ\xe9<\xfe\xff\xffVj\x01\x8dW\x18j\x01R\xe8p\x9f\x0e\x00\x83\xc4\x10\x83\xf8\x01t4\x8d\x84$\x14\x01\x00\x00\x8dL$\x14PQj\x00j\x00h\xb05\xa8\x00\xe8N\x90\x0e\x00\x83\xc4\x14\x8d\x94$\x14\x01\x00\x00\x8dD$\x14h+\x01\x00\x00R\xe9\xf1\xfd\xff\xff\x8bE\x003\xed;\xc5tC\xc1\xe0\x04P\xe8j\x91\x0e\x00\x83\xc4\x04;\xc5\x89G\x0cu0\x8d\x8c$\x14\x01\x00\x00\x8dT$\x14QRUUh\xb05\xa8\x00\xe8\x00\x90\x0e\x00\x83\xc4\x14\x8d\x84$\x14\x01\x00\x00\x8dL$\x14h7\x01\x00\x00PQ\xebN\x8b\x03;\xc5\x0f\x84\xc9\x00\x00\x00\x8bW\x0cVPj\x10R\xe8\xd1\x9e\x0e\x00\x8b\x0b\x83\xc4\x10;\xc1to\x8d\x84$\x14\x01\x00\x00\x8dL$\x14PQUUh\xb05\xa8\x00\xe8\xb0\x8f\x0e\x00\x83\xc4\x14\x8d\x94$\x14\x01\x00\x00\x8dD$\x14hE\x01\x00\x00RP\xe8\xc7\x13\x0b\x00Ph\xe8p\x98\x00h\xd0$\xae\x00\xe8&\x8f\x0e\x00Uh\xd0$\xae\x00h\xe0p\x98\x00\xe8\x07\xc5\xd8\xffV\x89-4z\xb6\x00\xe8t\x9c\x0e\x00\x83\xc4(_^]3\xc0[\x81\xc4\x04\x02\x00\x00\xc3;\xcd\x89l$\x10~<3\xd2\x8bO\x0c\x8bD\x11\x0c\x8dL\x11\x0c\x83\xf8\xfft\x16\x8d,\x80\xc1\xe5\x03+\xe8\xa1\xb0#\xae\x00\x8d\x04h3\xed\x89\x01\xeb\x02\x89)\x8bD$\x10\x8b\x0b#\x83\xc2\x10;\xc1\x89D$\x10|\xc6\xa1\x10q\xdd\x00\x993\xc2+\xc2=)\xe0\x03\x00\x0f\x8c\x80\x00\x00\x00Vj\x01j\x04h\xa45\xa8\x00\xe8\xf1\x9d\x0e\x00\x83\xc4\x10\x83\xf8\x01ti\x8d\x8c$\x14\x01\x00\x00\x8dT$\x14QRUUh\xb05\xa8\x00\xe8\xd1\x8e\x0e\x00\x83\xc4\x14\x8d\x84$\x14\x01\x00\x00\x8dL$\x14h\\\x01\x00\x00PQ\xe8\xe8\x12\x0b\x00Ph\xe8p\x98\x00h\xd0$\xae\x00\xe8G\x8e\x0e\x00Uh\xd0$\xae\x00h\xe0p\x98\x00\xe8(\xc4\xd8\xff\x83\xc4$\x89-4z\xb6\x003\xc0_^][\x81\xc4\x04\x02\x00\x00\xc3V\xe8\x85\x9b\x0e\x00\x83\xc4\x04\xb8\x01\x00\x00\x00_^][\x81\xc4\x04\x02\x00\x00\xc3\x90\x90\x90\x90\x90\x90\x90\x90\x90\x81\xec\x04\x02\x00\x00SUVWh(\x7f\x9a\x00h\xe05\xa8\x00\x8b\xf1h\xa0}\x98\x00\xe8 \xff\n'
They are both the same.
What am I doing wrong? Why can I not see a change in the readline()?
#Ethan Furman
offset1 = 0x472077
offset2 = 0x472078
offset3 = 0x472079
offset4 = 0x47208a
offset5 = 0x47208b
offset6 = 0x47208c
newvalue1 = b'\xe9'
newvalue2 = b'\xe9'
newvalue3 = b'\x00'
newvalue4 = b'\x00'
newvalue5 = b'\x00'
newvalue6 = b'\x90'
#originals
#offset old new
#0x472077 0xf 0xe9
#0x472078 0x85 0xe9
#0x472079 0xe8 0x0
#0x47208a 0x0 0x0
#0x47208b 0x0 0x0
#0x47208c 0x0 0x90
with open('test.exe', 'r+b') as victim:
victim.seek(offset1)
victim.write(newvalue1)
victim.seek(offset2)
victim.write(newvalue2)
victim.seek(offset3)
victim.write(newvalue3)
victim.seek(offset4)
victim.write(newvalue4)
victim.seek(offset5)
victim.write(newvalue5)
victim.seek(offset6)
victim.write(newvalue6)
The hard part is not changing the file, it's know what the codes are:
NOP = ???
OFFSET = 0x0045b82c
with open('some.exe', 'r+b') as victim:
victim.seek(OFFSET)
victim.write(NOP * 2)

Python simulate keydown

After searching for several hours i´m wondering if its possible to simulate a keydown press on the keyboard. For example I want my program to hold the x key down for five seconds so when I run it in notepad it would look like to see something like this: xxxxxxxxxxxxx. I tried around with different pieces of code on the internet, the best thing I could find so far is this:
import ctypes
import time
user32 = ctypes.windll.user32
inputhex = raw_input("Please enter your desired key's code (HEX): ")
keycode = int(inputhex, 16)
time.sleep(1)
#VOID keybd_event(BYTE bVk, BYTE bScan, DWORD dwFlags, PTR dwExtraInfo);
user32.keybd_event(keycode,0,2,0) #is the code for KEYDOWN
time.sleep(5)
#user32.keybd_event(keycode,0,0,0) #is the code for KEYDUP[/code]
The Sendkey module doesn't solve my problem either becuase it only allows you to send a single keypress and not a hold key down event. I know about autoit, used it in the past, but I just really want to know if this is possible with python and how.
P.S. i'm using python for windows
This code should get you started. ctypes is used heavily. At the bottom, you will see example code.
import ctypes
LONG = ctypes.c_long
DWORD = ctypes.c_ulong
ULONG_PTR = ctypes.POINTER(DWORD)
WORD = ctypes.c_ushort
class MOUSEINPUT(ctypes.Structure):
_fields_ = (('dx', LONG),
('dy', LONG),
('mouseData', DWORD),
('dwFlags', DWORD),
('time', DWORD),
('dwExtraInfo', ULONG_PTR))
class KEYBDINPUT(ctypes.Structure):
_fields_ = (('wVk', WORD),
('wScan', WORD),
('dwFlags', DWORD),
('time', DWORD),
('dwExtraInfo', ULONG_PTR))
class HARDWAREINPUT(ctypes.Structure):
_fields_ = (('uMsg', DWORD),
('wParamL', WORD),
('wParamH', WORD))
class _INPUTunion(ctypes.Union):
_fields_ = (('mi', MOUSEINPUT),
('ki', KEYBDINPUT),
('hi', HARDWAREINPUT))
class INPUT(ctypes.Structure):
_fields_ = (('type', DWORD),
('union', _INPUTunion))
def SendInput(*inputs):
nInputs = len(inputs)
LPINPUT = INPUT * nInputs
pInputs = LPINPUT(*inputs)
cbSize = ctypes.c_int(ctypes.sizeof(INPUT))
return ctypes.windll.user32.SendInput(nInputs, pInputs, cbSize)
INPUT_MOUSE = 0
INPUT_KEYBOARD = 1
INPUT_HARDWARE = 2
def Input(structure):
if isinstance(structure, MOUSEINPUT):
return INPUT(INPUT_MOUSE, _INPUTunion(mi=structure))
if isinstance(structure, KEYBDINPUT):
return INPUT(INPUT_KEYBOARD, _INPUTunion(ki=structure))
if isinstance(structure, HARDWAREINPUT):
return INPUT(INPUT_HARDWARE, _INPUTunion(hi=structure))
raise TypeError('Cannot create INPUT structure!')
WHEEL_DELTA = 120
XBUTTON1 = 0x0001
XBUTTON2 = 0x0002
MOUSEEVENTF_ABSOLUTE = 0x8000
MOUSEEVENTF_HWHEEL = 0x01000
MOUSEEVENTF_MOVE = 0x0001
MOUSEEVENTF_MOVE_NOCOALESCE = 0x2000
MOUSEEVENTF_LEFTDOWN = 0x0002
MOUSEEVENTF_LEFTUP = 0x0004
MOUSEEVENTF_RIGHTDOWN = 0x0008
MOUSEEVENTF_RIGHTUP = 0x0010
MOUSEEVENTF_MIDDLEDOWN = 0x0020
MOUSEEVENTF_MIDDLEUP = 0x0040
MOUSEEVENTF_VIRTUALDESK = 0x4000
MOUSEEVENTF_WHEEL = 0x0800
MOUSEEVENTF_XDOWN = 0x0080
MOUSEEVENTF_XUP = 0x0100
def MouseInput(flags, x, y, data):
return MOUSEINPUT(x, y, data, flags, 0, None)
VK_LBUTTON = 0x01 # Left mouse button
VK_RBUTTON = 0x02 # Right mouse button
VK_CANCEL = 0x03 # Control-break processing
VK_MBUTTON = 0x04 # Middle mouse button (three-button mouse)
VK_XBUTTON1 = 0x05 # X1 mouse button
VK_XBUTTON2 = 0x06 # X2 mouse button
VK_BACK = 0x08 # BACKSPACE key
VK_TAB = 0x09 # TAB key
VK_CLEAR = 0x0C # CLEAR key
VK_RETURN = 0x0D # ENTER key
VK_SHIFT = 0x10 # SHIFT key
VK_CONTROL = 0x11 # CTRL key
VK_MENU = 0x12 # ALT key
VK_PAUSE = 0x13 # PAUSE key
VK_CAPITAL = 0x14 # CAPS LOCK key
VK_KANA = 0x15 # IME Kana mode
VK_HANGUL = 0x15 # IME Hangul mode
VK_JUNJA = 0x17 # IME Junja mode
VK_FINAL = 0x18 # IME final mode
VK_HANJA = 0x19 # IME Hanja mode
VK_KANJI = 0x19 # IME Kanji mode
VK_ESCAPE = 0x1B # ESC key
VK_CONVERT = 0x1C # IME convert
VK_NONCONVERT = 0x1D # IME nonconvert
VK_ACCEPT = 0x1E # IME accept
VK_MODECHANGE = 0x1F # IME mode change request
VK_SPACE = 0x20 # SPACEBAR
VK_PRIOR = 0x21 # PAGE UP key
VK_NEXT = 0x22 # PAGE DOWN key
VK_END = 0x23 # END key
VK_HOME = 0x24 # HOME key
VK_LEFT = 0x25 # LEFT ARROW key
VK_UP = 0x26 # UP ARROW key
VK_RIGHT = 0x27 # RIGHT ARROW key
VK_DOWN = 0x28 # DOWN ARROW key
VK_SELECT = 0x29 # SELECT key
VK_PRINT = 0x2A # PRINT key
VK_EXECUTE = 0x2B # EXECUTE key
VK_SNAPSHOT = 0x2C # PRINT SCREEN key
VK_INSERT = 0x2D # INS key
VK_DELETE = 0x2E # DEL key
VK_HELP = 0x2F # HELP key
VK_LWIN = 0x5B # Left Windows key (Natural keyboard)
VK_RWIN = 0x5C # Right Windows key (Natural keyboard)
VK_APPS = 0x5D # Applications key (Natural keyboard)
VK_SLEEP = 0x5F # Computer Sleep key
VK_NUMPAD0 = 0x60 # Numeric keypad 0 key
VK_NUMPAD1 = 0x61 # Numeric keypad 1 key
VK_NUMPAD2 = 0x62 # Numeric keypad 2 key
VK_NUMPAD3 = 0x63 # Numeric keypad 3 key
VK_NUMPAD4 = 0x64 # Numeric keypad 4 key
VK_NUMPAD5 = 0x65 # Numeric keypad 5 key
VK_NUMPAD6 = 0x66 # Numeric keypad 6 key
VK_NUMPAD7 = 0x67 # Numeric keypad 7 key
VK_NUMPAD8 = 0x68 # Numeric keypad 8 key
VK_NUMPAD9 = 0x69 # Numeric keypad 9 key
VK_MULTIPLY = 0x6A # Multiply key
VK_ADD = 0x6B # Add key
VK_SEPARATOR = 0x6C # Separator key
VK_SUBTRACT = 0x6D # Subtract key
VK_DECIMAL = 0x6E # Decimal key
VK_DIVIDE = 0x6F # Divide key
VK_F1 = 0x70 # F1 key
VK_F2 = 0x71 # F2 key
VK_F3 = 0x72 # F3 key
VK_F4 = 0x73 # F4 key
VK_F5 = 0x74 # F5 key
VK_F6 = 0x75 # F6 key
VK_F7 = 0x76 # F7 key
VK_F8 = 0x77 # F8 key
VK_F9 = 0x78 # F9 key
VK_F10 = 0x79 # F10 key
VK_F11 = 0x7A # F11 key
VK_F12 = 0x7B # F12 key
VK_F13 = 0x7C # F13 key
VK_F14 = 0x7D # F14 key
VK_F15 = 0x7E # F15 key
VK_F16 = 0x7F # F16 key
VK_F17 = 0x80 # F17 key
VK_F18 = 0x81 # F18 key
VK_F19 = 0x82 # F19 key
VK_F20 = 0x83 # F20 key
VK_F21 = 0x84 # F21 key
VK_F22 = 0x85 # F22 key
VK_F23 = 0x86 # F23 key
VK_F24 = 0x87 # F24 key
VK_NUMLOCK = 0x90 # NUM LOCK key
VK_SCROLL = 0x91 # SCROLL LOCK key
VK_LSHIFT = 0xA0 # Left SHIFT key
VK_RSHIFT = 0xA1 # Right SHIFT key
VK_LCONTROL = 0xA2 # Left CONTROL key
VK_RCONTROL = 0xA3 # Right CONTROL key
VK_LMENU = 0xA4 # Left MENU key
VK_RMENU = 0xA5 # Right MENU key
VK_BROWSER_BACK = 0xA6 # Browser Back key
VK_BROWSER_FORWARD = 0xA7 # Browser Forward key
VK_BROWSER_REFRESH = 0xA8 # Browser Refresh key
VK_BROWSER_STOP = 0xA9 # Browser Stop key
VK_BROWSER_SEARCH = 0xAA # Browser Search key
VK_BROWSER_FAVORITES = 0xAB # Browser Favorites key
VK_BROWSER_HOME = 0xAC # Browser Start and Home key
VK_VOLUME_MUTE = 0xAD # Volume Mute key
VK_VOLUME_DOWN = 0xAE # Volume Down key
VK_VOLUME_UP = 0xAF # Volume Up key
VK_MEDIA_NEXT_TRACK = 0xB0 # Next Track key
VK_MEDIA_PREV_TRACK = 0xB1 # Previous Track key
VK_MEDIA_STOP = 0xB2 # Stop Media key
VK_MEDIA_PLAY_PAUSE = 0xB3 # Play/Pause Media key
VK_LAUNCH_MAIL = 0xB4 # Start Mail key
VK_LAUNCH_MEDIA_SELECT = 0xB5 # Select Media key
VK_LAUNCH_APP1 = 0xB6 # Start Application 1 key
VK_LAUNCH_APP2 = 0xB7 # Start Application 2 key
VK_OEM_1 = 0xBA # Used for miscellaneous characters; it can vary by keyboard.
# For the US standard keyboard, the ';:' key
VK_OEM_PLUS = 0xBB # For any country/region, the '+' key
VK_OEM_COMMA = 0xBC # For any country/region, the ',' key
VK_OEM_MINUS = 0xBD # For any country/region, the '-' key
VK_OEM_PERIOD = 0xBE # For any country/region, the '.' key
VK_OEM_2 = 0xBF # Used for miscellaneous characters; it can vary by keyboard.
# For the US standard keyboard, the '/?' key
VK_OEM_3 = 0xC0 # Used for miscellaneous characters; it can vary by keyboard.
# For the US standard keyboard, the '`~' key
VK_OEM_4 = 0xDB # Used for miscellaneous characters; it can vary by keyboard.
# For the US standard keyboard, the '[{' key
VK_OEM_5 = 0xDC # Used for miscellaneous characters; it can vary by keyboard.
# For the US standard keyboard, the '\|' key
VK_OEM_6 = 0xDD # Used for miscellaneous characters; it can vary by keyboard.
# For the US standard keyboard, the ']}' key
VK_OEM_7 = 0xDE # Used for miscellaneous characters; it can vary by keyboard.
# For the US standard keyboard, the 'single-quote/double-quote' key
VK_OEM_8 = 0xDF # Used for miscellaneous characters; it can vary by keyboard.
VK_OEM_102 = 0xE2 # Either the angle bracket key or the backslash key on the RT 102-key keyboard
VK_PROCESSKEY = 0xE5 # IME PROCESS key
VK_PACKET = 0xE7 # Used to pass Unicode characters as if they were keystrokes. The VK_PACKET key is the low word of a 32-bit Virtual Key value used for non-keyboard input methods. For more information, see Remark in KEYBDINPUT, SendInput, WM_KEYDOWN, and WM_KEYUP
VK_ATTN = 0xF6 # Attn key
VK_CRSEL = 0xF7 # CrSel key
VK_EXSEL = 0xF8 # ExSel key
VK_EREOF = 0xF9 # Erase EOF key
VK_PLAY = 0xFA # Play key
VK_ZOOM = 0xFB # Zoom key
VK_PA1 = 0xFD # PA1 key
VK_OEM_CLEAR = 0xFE # Clear key
KEYEVENTF_EXTENDEDKEY = 0x0001
KEYEVENTF_KEYUP = 0x0002
KEYEVENTF_SCANCODE = 0x0008
KEYEVENTF_UNICODE = 0x0004
KEY_0 = 0x30
KEY_1 = 0x31
KEY_2 = 0x32
KEY_3 = 0x33
KEY_4 = 0x34
KEY_5 = 0x35
KEY_6 = 0x36
KEY_7 = 0x37
KEY_8 = 0x38
KEY_9 = 0x39
KEY_A = 0x41
KEY_B = 0x42
KEY_C = 0x43
KEY_D = 0x44
KEY_E = 0x45
KEY_F = 0x46
KEY_G = 0x47
KEY_H = 0x48
KEY_I = 0x49
KEY_J = 0x4A
KEY_K = 0x4B
KEY_L = 0x4C
KEY_M = 0x4D
KEY_N = 0x4E
KEY_O = 0x4F
KEY_P = 0x50
KEY_Q = 0x51
KEY_R = 0x52
KEY_S = 0x53
KEY_T = 0x54
KEY_U = 0x55
KEY_V = 0x56
KEY_W = 0x57
KEY_X = 0x58
KEY_Y = 0x59
KEY_Z = 0x5A
def KeybdInput(code, flags):
return KEYBDINPUT(code, code, flags, 0, None)
def HardwareInput(message, parameter):
return HARDWAREINPUT(message & 0xFFFFFFFF,
parameter & 0xFFFF,
parameter >> 16 & 0xFFFF)
def Mouse(flags, x=0, y=0, data=0):
return Input(MouseInput(flags, x, y, data))
def Keyboard(code, flags=0):
return Input(KeybdInput(code, flags))
def Hardware(message, parameter=0):
return Input(HardwareInput(message, parameter))
################################################################################
import string
UPPER = frozenset('~!##$%^&*()_+QWERTYUIOP{}|ASDFGHJKL:"ZXCVBNM<>?')
LOWER = frozenset("`1234567890-=qwertyuiop[]\\asdfghjkl;'zxcvbnm,./")
ORDER = string.ascii_letters + string.digits + ' \b\r\t'
ALTER = dict(zip('!##$%^&*()', '1234567890'))
OTHER = {'`': VK_OEM_3,
'~': VK_OEM_3,
'-': VK_OEM_MINUS,
'_': VK_OEM_MINUS,
'=': VK_OEM_PLUS,
'+': VK_OEM_PLUS,
'[': VK_OEM_4,
'{': VK_OEM_4,
']': VK_OEM_6,
'}': VK_OEM_6,
'\\': VK_OEM_5,
'|': VK_OEM_5,
';': VK_OEM_1,
':': VK_OEM_1,
"'": VK_OEM_7,
'"': VK_OEM_7,
',': VK_OEM_COMMA,
'<': VK_OEM_COMMA,
'.': VK_OEM_PERIOD,
'>': VK_OEM_PERIOD,
'/': VK_OEM_2,
'?': VK_OEM_2}
def keyboard_stream(string):
mode = False
for character in string.replace('\r\n', '\r').replace('\n', '\r'):
if mode and character in LOWER or not mode and character in UPPER:
yield Keyboard(VK_SHIFT, mode and KEYEVENTF_KEYUP)
mode = not mode
character = ALTER.get(character, character)
if character in ORDER:
code = ord(character.upper())
elif character in OTHER:
code = OTHER[character]
else:
continue
#Or, to abort on unavailable character
#raise ValueError('String is not understood!')
yield Keyboard(code)
yield Keyboard(code, KEYEVENTF_KEYUP)
if mode:
yield Keyboard(VK_SHIFT, KEYEVENTF_KEYUP)
################################################################################
import time, sys
def main():
time.sleep(5)
for event in keyboard_stream('o2E^uXh#:SHn&HQ+t]YF'):
SendInput(event)
time.sleep(0.1)
##if __name__ == '__main__':
## main()
def switch_program():
SendInput(Keyboard(VK_MENU), Keyboard(VK_TAB))
time.sleep(0.2)
SendInput(Keyboard(VK_TAB, KEYEVENTF_KEYUP),
Keyboard(VK_MENU, KEYEVENTF_KEYUP))
time.sleep(0.2)
def select_line():
SendInput(Keyboard(VK_SHIFT, KEYEVENTF_EXTENDEDKEY),
Keyboard(VK_END, KEYEVENTF_EXTENDEDKEY))
time.sleep(0.2)
SendInput(Keyboard(VK_SHIFT, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP),
Keyboard(VK_END, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP))
time.sleep(0.2)
def copy_line():
SendInput(Keyboard(VK_CONTROL), Keyboard(KEY_C))
time.sleep(0.2)
SendInput(Keyboard(VK_CONTROL, KEYEVENTF_KEYUP),
Keyboard(KEY_C, KEYEVENTF_KEYUP))
time.sleep(0.2)
def next_line():
SendInput(Keyboard(VK_HOME), Keyboard(VK_DOWN))
time.sleep(0.2)
SendInput(Keyboard(VK_HOME, KEYEVENTF_KEYUP),
Keyboard(VK_DOWN, KEYEVENTF_KEYUP))
time.sleep(0.2)
def prepare_text():
# Open Text
SendInput(Keyboard(KEY_M))
time.sleep(0.2)
SendInput(Keyboard(KEY_M, KEYEVENTF_KEYUP))
time.sleep(0.2)
# Goto Area
SendInput(Keyboard(VK_TAB))
time.sleep(0.2)
SendInput(Keyboard(VK_TAB, KEYEVENTF_KEYUP))
time.sleep(0.2)
# Paste Message
SendInput(Keyboard(VK_CONTROL), Keyboard(KEY_V))
time.sleep(0.2)
SendInput(Keyboard(VK_CONTROL, KEYEVENTF_KEYUP),
Keyboard(KEY_V, KEYEVENTF_KEYUP))
time.sleep(0.2)
# Goto Button
SendInput(Keyboard(VK_TAB))
time.sleep(0.2)
SendInput(Keyboard(VK_TAB, KEYEVENTF_KEYUP))
time.sleep(0.2)
def send_one_message():
select_line()
copy_line()
next_line()
switch_program()
prepare_text()
# Send Message
SendInput(Keyboard(VK_RETURN))
time.sleep(0.2)
SendInput(Keyboard(VK_RETURN, KEYEVENTF_KEYUP))
time.sleep(10)
switch_program()
def send_messages(total):
time.sleep(10)
for _ in range(total):
send_one_message()
If you're using Python for Windows then there's a very good chance that you have the win32api module, which handles hooking into the API for you...
import win32api
import win32con
win32api.keybd_event(win32con.SHIFT_PRESSED, 0,win32con.KEYEVENTF_EXTENDEDKEY, 0)
Does that help? (p.s. you should install IPython if, at all possible, it's massively helpful for experimenting)
I know that this question is really old, but I found a nice library: PyUserInput.
I am not sure how to download it in windows(I haven't downloaded libraries for windows for a long time). In Linux is easy just download pip(if you haven't):sudo apt-get install pip and then sudo pip install pyuserinput.
And then the code is realy simple:
#imports the library
from pykeyboard import PyKeyboard
#import the sleep function
from time import sleep
#initialize the keyboard simulator
keyboard = PyKeyboard()
#presses the key
keyboard.press_key('x')
#waits five seconds before releasing the key
sleep(5)
#releases the key
keyboard.release_key('x')
You can use pynput library which is easy to use.
from pynput.keyboard import Key, Listener, Controller as keyboard_controller
keyboard = keyboard_controller()
keyboard.type("Please enter your desired key's code (HEX): ")
You can also simulate mouse even with this library.
I've modified the Noctis answer to work with Unicode characters.
"Simple unicode keyboard automation for windows"
https://gist.github.com/ubershmekel/4b414a66037feaea595b5f4e78220aad
The above link has a gist, this is the code from that gist today:
# coding: utf-8
"""
Simple unicode keyboard automation for windows
Based off of http://stackoverflow.com/questions/11906925/python-simulate-keydown
"""
import ctypes
import time
import sys
LONG = ctypes.c_long
DWORD = ctypes.c_ulong
ULONG_PTR = ctypes.POINTER(DWORD)
WORD = ctypes.c_ushort
INPUT_MOUSE = 0
INPUT_KEYBOARD = 1
INPUT_HARDWARE = 2
KEYEVENTF_EXTENDEDKEY = 0x0001
KEYEVENTF_KEYUP = 0x0002
KEYEVENTF_SCANCODE = 0x0008
KEYEVENTF_UNICODE = 0x0004
class MOUSEINPUT(ctypes.Structure):
_fields_ = (('dx', LONG),
('dy', LONG),
('mouseData', DWORD),
('dwFlags', DWORD),
('time', DWORD),
('dwExtraInfo', ULONG_PTR))
class KEYBDINPUT(ctypes.Structure):
_fields_ = (('wVk', WORD),
('wScan', WORD),
('dwFlags', DWORD),
('time', DWORD),
('dwExtraInfo', ULONG_PTR))
class HARDWAREINPUT(ctypes.Structure):
_fields_ = (('uMsg', DWORD),
('wParamL', WORD),
('wParamH', WORD))
class _INPUTunion(ctypes.Union):
_fields_ = (('mi', MOUSEINPUT),
('ki', KEYBDINPUT),
('hi', HARDWAREINPUT))
class INPUT(ctypes.Structure):
_fields_ = (('type', DWORD),
('union', _INPUTunion))
def send_input(*inputs):
nInputs = len(inputs)
LPINPUT = INPUT * nInputs
pInputs = LPINPUT(*inputs)
cbSize = ctypes.c_int(ctypes.sizeof(INPUT))
return ctypes.windll.user32.SendInput(nInputs, pInputs, cbSize)
def input_structure(structure):
if isinstance(structure, MOUSEINPUT):
return INPUT(INPUT_MOUSE, _INPUTunion(mi=structure))
if isinstance(structure, KEYBDINPUT):
return INPUT(INPUT_KEYBOARD, _INPUTunion(ki=structure))
if isinstance(structure, HARDWAREINPUT):
return INPUT(INPUT_HARDWARE, _INPUTunion(hi=structure))
raise TypeError('Cannot create INPUT structure!')
def keyboard_input(code, flags):
return KEYBDINPUT(0, code, flags, 0, None)
def keyboard_event(code, flags=KEYEVENTF_UNICODE):
return input_structure(keyboard_input(code, flags))
def press(character):
code = ord(character)
send_input(keyboard_event(code))
send_input(keyboard_event(code, KEYEVENTF_KEYUP))
def main():
time.sleep(3)
for char in u'O\nשש2E6UXoשש2E^uXh#:SHn&HQ':
press(char)
time.sleep(0.5)
if __name__ == '__main__':
main()

Categories

Resources