Send APDU Commands in python (RC522, RaspberryPi) - python

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.

Related

Overflow while using ReadWriteMemory

I'm trying to fetch information from a hex editor. But ReadWriteMemory gives me an error about "<class 'OverflowError'>: int too long to convert"
Here is my code:
from ReadWriteMemory import ReadWriteMemory
base_address = 0x7FF6D60A0000
static_address_offset = 0x0074DE40
pointer_static_address = base_address + static_address_offset
offsets = [0x08, 0x08, 0xB0, 0x08, 0x278, 0x10, 0xD0]
rmw = ReadWriteMemory()
process = rmw.get_process_by_name('010Editor.exe')
process.open()
pointer = process.get_pointer(pointer_static_address, offsets=offsets)
pointer_value = process.read(pointer)
print(pointer_value)
How can I solve this?
I fixed it using 'pymem', So it appears that ReadWriteMemory can't handle 64bit applications.
from pymem import *
from pymem.process import *
pm = Pymem('010Editor.exe')
def GetByteNumber():
def GetPtrAddr(base, offsets):
addr = pm.read_longlong(base)
for i in offsets:
if i != offsets[-1]:
addr = pm.read_longlong(addr + i)
return addr + offsets[-1]
return pm.read_int(GetPtrAddr(pm.base_address + 0x0074DE40, offsets=[0x8, 0x8, 0x60, 0x8, 0x10, 0xB0, 0xD0]))

Remove NFC Tag in Python

I got my hands on a Lego Dimensions pad which is in fact a triple NFC reader. Found some code on github to start playing with it with a raspberry pi. I've got it so far that it sends a webhook to my Home Assistant to turn on a light when placing a minifigure on the pad. Where I got stuck and my python programming skills fall short is to also send a webhook when the tag is removed. Can anyone point me in the right direction?
Thx!
#!/usr/bin/python
import usb.core
import usb.util
from time import sleep
import requests
TOYPAD_INIT = [0x55, 0x0f, 0xb0, 0x01, 0x28, 0x63, 0x29, 0x20, 0x4c, 0x45, 0x47, 0x4f, 0x20, 0x32, 0x30, 0x31, 0x34, 0xf7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]
# Colors
OFF = [0,0,0]
RED = [255,0,0]
GREEN = [0,255,0]
BLUE = [0,0,255]
YELLOW = [255,255,0]
PURPLE = [128,0,128]
# Pad number
ALL_PADS = 0
CENTER_PAD = 1
LEFT_PAD = 2
RIGHT_PAD = 3
# Actions
TAG_INSERTED = 0
TAG_REMOVED = 1
# UIDs can be retrieved with Android App (most probably in hexadecimal)
uidBatman = [4, 117, 251, 42, 41, 73, 128] # Batman Lego
uidLucy = [4, 66, 84, 114, 82, 66, 129] # Lucy Lego
uidGandalf = [4, 133, 184, 90, 181, 73, 128] # Gandalf
# Webhook var
auth_token='[Token]'
hed = {'Authorization': 'Bearer ' + auth_token}
dataon = {"state": "on"}
dataoff = {"state": "off"}
def init_usb():
global dev
dev = usb.core.find(idVendor=0x0e6f, idProduct=0x0241)
if dev is None:
print("Device not found")
else:
if dev.is_kernel_driver_active(0):
dev.detach_kernel_driver(0)
print usb.util.get_string(dev, dev.iProduct)
dev.set_configuration()
dev.write(1,TOYPAD_INIT)
return dev
def send_command(dev,command):
# calculate checksum
checksum = 0
for word in command:
checksum = checksum + word
if checksum >= 256:
checksum -= 256
message = command+[checksum]
# pad message
while(len(message) < 32):
message.append(0x00)
# send message
dev.write(1, message)
return
def switch_pad(pad, colour):
send_command(dev,[0x55, 0x06, 0xc0, 0x02, pad, colour[0], colour[1], colour[2],])
return
def main():
init_usb()
if dev != None :
while True:
try:
in_packet = dev.read(0x81, 32, timeout = 10)
bytelist = list(in_packet)
if not bytelist:
pass
elif bytelist[0] != 0x56: # NFC packets start with 0x56
pass
else:
pad_num = bytelist[2]
uid_bytes = bytelist[6:13]
action = bytelist[5]
if action == TAG_INSERTED :
print uid_bytes
if (uid_bytes == uidBatman) :
switch_pad(pad_num, RED)
url = '[URL]'
response = requests.post(url, json=dataon, headers=hed)
print(response)
print(response.json())
elif (uid_bytes == uidLucy) :
switch_pad(pad_num, PURPLE)
url = '[URL]'
response = requests.post(url, json=data, headers=hed)
print(response)
print(response.json())
elif (uid_bytes == uidGandalf) :
switch_pad(pad_num, YELLOW)
url = '[URL]'
response = requests.post(url, json=data, headers=hed)
print(response)
print(response.json())
else:
# some other tag
switch_pad(pad_num, GREEN)
else:
# some tag removed
switch_pad(pad_num, OFF)
except usb.USBError, err:
pass
switch_pad(ALL_PADS,OFF)
return
if __name__ == '__main__':
main()

I wrote this python program and it keeps giving me errors

I wrote this and it keeps giving me errors:
import usb.util
from time import sleep
TOYPAD_INIT = [0x55, 0x0f, 0xb0, 0x01, 0x28, 0x63, 0x29, 0x20, 0x4c, 0x45, 0x47, 0x4f, 0x20, 0x32, 0x30, 0x31, 0x34, 0xf7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]
OFF = [0,0,0]
RED = [255,0,0]
GREEN = [0,255,0]
BLUE = [0,0,255]
ALL_PADS = 0
CENTER_PAD = 1
LEFT_PAD = 2
RIGHT_PAD = 3
# Actions
TAG_INSERTED = 0
TAG_REMOVED = 1
# UIDs can be retrieved with Android App (most probably in hexadecimal)
uidDarthVader = (4, 161, 158, 210, 227, 64 , 128) # Darth Vader from Disney Infinity 3.0
def init_usb():
global dev
dev = usb.core.find(idVendor=0x0e6f, idProduct=0x0241)
if dev is None:
print('Device not found')
else:
if dev.is_kernel_driver_active(0):
dev.detach_kernel_driver(0)
print(usb.util.get_string(dev, dev.iProduct))
dev.set_configuration()
dev.write(1,TOYPAD_INIT)
return dev
def send_command(dev,command):
# calculate checksum
checksum = 0
for word in command:
checksum = checksum + word
if checksum >= 256:
checksum -= 256
message = command+[checksum]
# pad message
while(len(message) < 32):
message.append(0x00)
# send message
dev.write(1, message)
return
def switch_pad(pad, colour):
send_command(dev,[0x55, 0x06, 0xc0, 0x02, pad, colour[0], colour[1], colour[2],])
return
def uid_compare(uid1, uid2):
match = True
for i in range(0,7):
if (uid1[i] != uid2[i]) :
match = False
return match
def main():
init_usb()
if dev != None :
while True:
try:
in_packet = dev.read(0x81, 32, timeout = 10)
bytelist = list(in_packet)
if not bytelist:
pass
elif bytelist[0] != 0x56: # NFC packets start with 0x56
pass
else:
pad_num = bytelist[2]
uid_bytes = bytelist[6:13]
match = uid_compare(uid_bytes, uidDarthVader)
action = bytelist[5]
if action == TAG_INSERTED :
if match:
# Darth Vader
switch_pad(pad_num, RED)
else:
# some other tag
switch_pad(pad_num, GREEN)
else:
# some tag removed
switch_pad(pad_num, OFF)
except(usb.USBError, err):
pass
switch_pad(ALL_PADS,OFF)
return
if __name__ == '__main__':
main()
and get one of two errors:
NameError: name 'err' is not defined
or
except usb.USBError, err:
^
SyntaxError: invalid syntax
why is that? My goal is to use this for a piece of tech i'm developing. i have tried looking on the web found nothing and tried reformatting the code still didn't work, finally i tried to use the code formatting assistant and it still didn't help.
Your error is a result of the variable err being undefined.
Here I can replicate the error:
In [7]: try:
...: raise SyntaxError
...: except(SyntaxError, err):
...: print("Error message")
...:
---------------------------------------------------------------------------
SyntaxError Traceback (most recent call last)
<ipython-input-7-7c18a68f8815> in <module>
1 try:
----> 2 raise SyntaxError
3 except(SyntaxError, err):
SyntaxError: None
During handling of the above exception, another exception occurred:
NameError Traceback (most recent call last)
<ipython-input-7-7c18a68f8815> in <module>
1 try:
2 raise SyntaxError
----> 3 except(SyntaxError, err):
4 print("Error message")
5
NameError: name 'err' is not defined
Instead, try removing the err, your except line should read
except(SyntaxError):
You should also know that the github repo where you got the code that you copy-pasted is in python 2, so that is likely why you are seeing this error.
Here is a link to the source, I've taken the liberty of fixing the syntax highlighting.

Weird behavior in Python in array initialization

In the following you see a snippet of my Python program that I wrote to send APDU commands to smart cards. I used PySCard library in this program.
First Program :
for LOAD_KEY in validLoadCommands:
data,sw1,sw2 =connection.transmit(LOAD_KEY)
temp = LOAD_KEY
x= temp[3]
for j in range(0,len(LOAD_KEY)):
LOAD_KEY[j]=hex(LOAD_KEY[j])
print ' >>>',' Load Command :',
for j in range(0,len(LOAD_KEY)):
LOAD_KEY[j]=str(LOAD_KEY[j])
if len(LOAD_KEY[j])==3:
LOAD_KEY[j]=LOAD_KEY[j][0:2]+'0'+LOAD_KEY[j][2]
print LOAD_KEY[j],
print
for j in range(0,len(data)):
data[j]=hex(data[j])
print ' <<<',' Data :',
for j in range(0,len(data)):
data[j]=str(data[j])
if len(data[j])==3:
data[j]=data[j][0:2]+'0'+data[j][2]
print data[j],
print '--',hex(sw1),hex(sw2)
if (temp[2] == 0x00 or temp[2] == 0x20):
keyType = 0x60
else:
keyType = 0x61
AUTH = [0xFF, 0x86, 0x00, 0x00, 0x05, 0x01, 0x00, blockNum, keyType,temp[3]]
print 'AUTH Command: ', AUTH
data,sw1,sw2 =connection.transmit(AUTH)
print data, sw1, sw2
And its output :
Auth Command: [255, 134, 0, 0, 5, 1, 0, 4, 97, '0x0e']
Traceback (most recent call last):
File "C:\Users\Erb4h1m\Desktop\Faraadis Alborz Card Reader\CRT-603-CZ1_Read1Block.py", line 199, in <module>
data,sw1,sw2 =connection.transmit(AUTH)
File "D:\Software\Python27\lib\site-packages\smartcard\CardConnectionDecorator.py", line 82, in transmit
return self.component.transmit(bytes, protocol)
File "D:\Software\Python27\lib\site-packages\smartcard\CardConnection.py", line 140, in transmit
data, sw1, sw2 = self.doTransmit(bytes, protocol)
File "D:\Software\Python27\lib\site-packages\smartcard\pcsc\PCSCCardConnection.py", line 173, in doTransmit
hresult, response = SCardTransmit(self.hcard, pcscprotocolheader, bytes)
File "D:\Software\Python27\lib\site-packages\smartcard\scard\scard.py", line 1329, in SCardTransmit
return _scard.SCardTransmit(*args)
TypeError: Expected a list of bytes.
And when I replace temp[3] with x:
for LOAD_KEY in validLoadCommands:
data,sw1,sw2 =connection.transmit(LOAD_KEY)
temp = LOAD_KEY
x= temp[3]
for j in range(0,len(LOAD_KEY)):
LOAD_KEY[j]=hex(LOAD_KEY[j])
print ' >>>',' Load Command :',
for j in range(0,len(LOAD_KEY)):
LOAD_KEY[j]=str(LOAD_KEY[j])
if len(LOAD_KEY[j])==3:
LOAD_KEY[j]=LOAD_KEY[j][0:2]+'0'+LOAD_KEY[j][2]
print LOAD_KEY[j],
print
for j in range(0,len(data)):
data[j]=hex(data[j])
print ' <<<',' Data :',
for j in range(0,len(data)):
data[j]=str(data[j])
if len(data[j])==3:
data[j]=data[j][0:2]+'0'+data[j][2]
print data[j],
print '--',hex(sw1),hex(sw2)
if (temp[2] == 0x00 or temp[2] == 0x20):
keyType = 0x60
else:
keyType = 0x61
AUTH = [0xFF, 0x86, 0x00, 0x00, 0x05, 0x01, 0x00, blockNum, keyType, x]
print 'AUTH Command: ', AUTH
data,sw1,sw2 =connection.transmit(AUTH)
print data, sw1, sw2
Then the output is:
AUTH: [255, 134, 0, 0, 5, 1, 0, 4, 97, 14]
[] 144 0
As you see above, in the second case I DON'T receive any error, while there is no difference between first program and second program! I only replaced
AUTH = [0xFF, 0x86, 0x00, 0x00, 0x05, 0x01, 0x00, blockNum, keyType,temp[3]]
with:
AUTH = [0xFF, 0x86, 0x00, 0x00, 0x05, 0x01, 0x00, blockNum, keyType,x]
and x is initialized with temp[3] in the top of program. neither x nor temp[3] didn't changed between the line of initialization and the line of error.
What is going here?
Your temp variable reference LOAD_KEY (Line 3: temp = LOAD_KEY)
When x= temp[3] get called, it's the value of LOAD_KEY at index 3 right after your connection.transmit that is set.
When you are sending temp[3] for your AUTH, LOAD_KEY have changed and so does temp[3]. So it's not anymore the same value as x.
connection.transmit => LOAD_KEY is set => temp => [3] => x
*** LOAD_KEY changes ***
x => old value of temp[3] aka LOAD_KEY[3]
temp[3] => whatever your code have put in it
Edit: short example
>>> LOAD_KEY = ['one', 'two', 'three', 'four']
>>> temp = LOAD_KEY
>>> x = temp[3]
>>> LOAD_KEY[3] = 'new value'
>>>
>>> LOAD_KEY[3]
'new value'
>>> temp[3]
'new value'
>>> x
'four'
In you first example, temp[3] is a string ('0x0e').
In your second example, x is an integer (14).
This is because you're converting the integer values of the LOAD_KEY list to an hexadecimal string with the following line:
LOAD_KEY[j] = hex(LOAD_KEY[j])
In order to convert a string in hexadecimal representation to an integer, use the second argument of the built-in type int to set the base to 16:
>>> int('0x0e', 16)
14

How to generate keyboard events?

short summary:
I am trying to create a program that will send keyboard events to the computer that for all purposes the simulated events should be treated as actual keystrokes on the keyboard.
original post:
I am looking for a way to generate keyboard events using python.
Assume that the function receives a key that it must simulate pressing, like so:
keyboardevent('a') #lower case 'a'
keyboardevent('B') #upper case 'B'
keyboardevent('->') # right arrow key
def keyboardevent(key):
#code that simulated 'key' being pressed on keyboard
The above are obviously examples, but what I am looking for is a library, module, or whatever, which I can use to simulate keyboard events.
note: This is different than sending characters to notepads, or inputting text into fields or such. I want the python script to simulate an actual keyboard event, the computer will think that there is really a keyboard event.
Extra Note:
I don't want to send keystrokes to the active window - I want the system to believe the keyboard's keys are being pressed, subtle difference, as some active-windows do not accept certain key-combinations, or if I wanted to use keyboard shortcuts for background processes through my script, they don't need to go through the active-window
So far I have looked at these things:
Generate keyboard events for the frontmost application
How to generate keyboard keypress events through Python?
Which were all about apple and didn't help at all.
And this:
Which is the easiest way to simulate keyboard and mouse on Python?
Which seems like it might be what I need, but I can not find the library for it or any documentation.
I have searched more places as well, but have yet to find a solution.
It can be done using ctypes:
import ctypes
from ctypes import wintypes
import time
user32 = ctypes.WinDLL('user32', use_last_error=True)
INPUT_MOUSE = 0
INPUT_KEYBOARD = 1
INPUT_HARDWARE = 2
KEYEVENTF_EXTENDEDKEY = 0x0001
KEYEVENTF_KEYUP = 0x0002
KEYEVENTF_UNICODE = 0x0004
KEYEVENTF_SCANCODE = 0x0008
MAPVK_VK_TO_VSC = 0
# msdn.microsoft.com/en-us/library/dd375731
VK_TAB = 0x09
VK_MENU = 0x12
# C struct definitions
wintypes.ULONG_PTR = wintypes.WPARAM
class MOUSEINPUT(ctypes.Structure):
_fields_ = (("dx", wintypes.LONG),
("dy", wintypes.LONG),
("mouseData", wintypes.DWORD),
("dwFlags", wintypes.DWORD),
("time", wintypes.DWORD),
("dwExtraInfo", wintypes.ULONG_PTR))
class KEYBDINPUT(ctypes.Structure):
_fields_ = (("wVk", wintypes.WORD),
("wScan", wintypes.WORD),
("dwFlags", wintypes.DWORD),
("time", wintypes.DWORD),
("dwExtraInfo", wintypes.ULONG_PTR))
def __init__(self, *args, **kwds):
super(KEYBDINPUT, self).__init__(*args, **kwds)
# some programs use the scan code even if KEYEVENTF_SCANCODE
# isn't set in dwFflags, so attempt to map the correct code.
if not self.dwFlags & KEYEVENTF_UNICODE:
self.wScan = user32.MapVirtualKeyExW(self.wVk,
MAPVK_VK_TO_VSC, 0)
class HARDWAREINPUT(ctypes.Structure):
_fields_ = (("uMsg", wintypes.DWORD),
("wParamL", wintypes.WORD),
("wParamH", wintypes.WORD))
class INPUT(ctypes.Structure):
class _INPUT(ctypes.Union):
_fields_ = (("ki", KEYBDINPUT),
("mi", MOUSEINPUT),
("hi", HARDWAREINPUT))
_anonymous_ = ("_input",)
_fields_ = (("type", wintypes.DWORD),
("_input", _INPUT))
LPINPUT = ctypes.POINTER(INPUT)
def _check_count(result, func, args):
if result == 0:
raise ctypes.WinError(ctypes.get_last_error())
return args
user32.SendInput.errcheck = _check_count
user32.SendInput.argtypes = (wintypes.UINT, # nInputs
LPINPUT, # pInputs
ctypes.c_int) # cbSize
# Functions
def PressKey(hexKeyCode):
x = INPUT(type=INPUT_KEYBOARD,
ki=KEYBDINPUT(wVk=hexKeyCode))
user32.SendInput(1, ctypes.byref(x), ctypes.sizeof(x))
def ReleaseKey(hexKeyCode):
x = INPUT(type=INPUT_KEYBOARD,
ki=KEYBDINPUT(wVk=hexKeyCode,
dwFlags=KEYEVENTF_KEYUP))
user32.SendInput(1, ctypes.byref(x), ctypes.sizeof(x))
def AltTab():
"""Press Alt+Tab and hold Alt key for 2 seconds
in order to see the overlay.
"""
PressKey(VK_MENU) # Alt
PressKey(VK_TAB) # Tab
ReleaseKey(VK_TAB) # Tab~
time.sleep(2)
ReleaseKey(VK_MENU) # Alt~
if __name__ == "__main__":
AltTab()
hexKeyCode is the virtual keyboard mapping as defined by the Windows API. The list of codes is available on MSDN: Virtual-Key Codes (Windows)
For both python3 and python2 you can use pyautogui (pip install pyautogui)
from pyautogui import press, typewrite, hotkey
press('a')
typewrite('quick brown fox')
hotkey('ctrl', 'w')
It's also crossplatform with Windows, OSX, and Ubuntu LTS.
I tried lib keyboard and it works good on Windows, Mac and Linux. Below line helps me switch tabs in browser:
keyboard.press_and_release('ctrl+tab')
user648852's idea at least for me works great for OS X, here is the code to do it:
#!/usr/bin/env python
import time
from Quartz.CoreGraphics import CGEventCreateKeyboardEvent
from Quartz.CoreGraphics import CGEventPost
# Python releases things automatically, using CFRelease will result in a scary error
#from Quartz.CoreGraphics import CFRelease
from Quartz.CoreGraphics import kCGHIDEventTap
# From http://stackoverflow.com/questions/281133/controlling-the-mouse-from-python-in-os-x
# and from https://developer.apple.com/library/mac/documentation/Carbon/Reference/QuartzEventServicesRef/index.html#//apple_ref/c/func/CGEventCreateKeyboardEvent
def KeyDown(k):
keyCode, shiftKey = toKeyCode(k)
time.sleep(0.0001)
if shiftKey:
CGEventPost(kCGHIDEventTap, CGEventCreateKeyboardEvent(None, 0x38, True))
time.sleep(0.0001)
CGEventPost(kCGHIDEventTap, CGEventCreateKeyboardEvent(None, keyCode, True))
time.sleep(0.0001)
if shiftKey:
CGEventPost(kCGHIDEventTap, CGEventCreateKeyboardEvent(None, 0x38, False))
time.sleep(0.0001)
def KeyUp(k):
keyCode, shiftKey = toKeyCode(k)
time.sleep(0.0001)
CGEventPost(kCGHIDEventTap, CGEventCreateKeyboardEvent(None, keyCode, False))
time.sleep(0.0001)
def KeyPress(k):
keyCode, shiftKey = toKeyCode(k)
time.sleep(0.0001)
if shiftKey:
CGEventPost(kCGHIDEventTap, CGEventCreateKeyboardEvent(None, 0x38, True))
time.sleep(0.0001)
CGEventPost(kCGHIDEventTap, CGEventCreateKeyboardEvent(None, keyCode, True))
time.sleep(0.0001)
CGEventPost(kCGHIDEventTap, CGEventCreateKeyboardEvent(None, keyCode, False))
time.sleep(0.0001)
if shiftKey:
CGEventPost(kCGHIDEventTap, CGEventCreateKeyboardEvent(None, 0x38, False))
time.sleep(0.0001)
# From http://stackoverflow.com/questions/3202629/where-can-i-find-a-list-of-mac-virtual-key-codes
def toKeyCode(c):
shiftKey = False
# Letter
if c.isalpha():
if not c.islower():
shiftKey = True
c = c.lower()
if c in shiftChars:
shiftKey = True
c = shiftChars[c]
if c in keyCodeMap:
keyCode = keyCodeMap[c]
else:
keyCode = ord(c)
return keyCode, shiftKey
shiftChars = {
'~': '`',
'!': '1',
'#': '2',
'#': '3',
'$': '4',
'%': '5',
'^': '6',
'&': '7',
'*': '8',
'(': '9',
')': '0',
'_': '-',
'+': '=',
'{': '[',
'}': ']',
'|': '\\',
':': ';',
'"': '\'',
'<': ',',
'>': '.',
'?': '/'
}
keyCodeMap = {
'a' : 0x00,
's' : 0x01,
'd' : 0x02,
'f' : 0x03,
'h' : 0x04,
'g' : 0x05,
'z' : 0x06,
'x' : 0x07,
'c' : 0x08,
'v' : 0x09,
'b' : 0x0B,
'q' : 0x0C,
'w' : 0x0D,
'e' : 0x0E,
'r' : 0x0F,
'y' : 0x10,
't' : 0x11,
'1' : 0x12,
'2' : 0x13,
'3' : 0x14,
'4' : 0x15,
'6' : 0x16,
'5' : 0x17,
'=' : 0x18,
'9' : 0x19,
'7' : 0x1A,
'-' : 0x1B,
'8' : 0x1C,
'0' : 0x1D,
']' : 0x1E,
'o' : 0x1F,
'u' : 0x20,
'[' : 0x21,
'i' : 0x22,
'p' : 0x23,
'l' : 0x25,
'j' : 0x26,
'\'' : 0x27,
'k' : 0x28,
';' : 0x29,
'\\' : 0x2A,
',' : 0x2B,
'/' : 0x2C,
'n' : 0x2D,
'm' : 0x2E,
'.' : 0x2F,
'`' : 0x32,
'k.' : 0x41,
'k*' : 0x43,
'k+' : 0x45,
'kclear' : 0x47,
'k/' : 0x4B,
'k\n' : 0x4C,
'k-' : 0x4E,
'k=' : 0x51,
'k0' : 0x52,
'k1' : 0x53,
'k2' : 0x54,
'k3' : 0x55,
'k4' : 0x56,
'k5' : 0x57,
'k6' : 0x58,
'k7' : 0x59,
'k8' : 0x5B,
'k9' : 0x5C,
# keycodes for keys that are independent of keyboard layout
'\n' : 0x24,
'\t' : 0x30,
' ' : 0x31,
'del' : 0x33,
'delete' : 0x33,
'esc' : 0x35,
'escape' : 0x35,
'cmd' : 0x37,
'command' : 0x37,
'shift' : 0x38,
'caps lock' : 0x39,
'option' : 0x3A,
'ctrl' : 0x3B,
'control' : 0x3B,
'right shift' : 0x3C,
'rshift' : 0x3C,
'right option' : 0x3D,
'roption' : 0x3D,
'right control' : 0x3E,
'rcontrol' : 0x3E,
'fun' : 0x3F,
'function' : 0x3F,
'f17' : 0x40,
'volume up' : 0x48,
'volume down' : 0x49,
'mute' : 0x4A,
'f18' : 0x4F,
'f19' : 0x50,
'f20' : 0x5A,
'f5' : 0x60,
'f6' : 0x61,
'f7' : 0x62,
'f3' : 0x63,
'f8' : 0x64,
'f9' : 0x65,
'f11' : 0x67,
'f13' : 0x69,
'f16' : 0x6A,
'f14' : 0x6B,
'f10' : 0x6D,
'f12' : 0x6F,
'f15' : 0x71,
'help' : 0x72,
'home' : 0x73,
'pgup' : 0x74,
'page up' : 0x74,
'forward delete' : 0x75,
'f4' : 0x76,
'end' : 0x77,
'f2' : 0x78,
'page down' : 0x79,
'pgdn' : 0x79,
'f1' : 0x7A,
'left' : 0x7B,
'right' : 0x7C,
'down' : 0x7D,
'up' : 0x7E
}
I had this same problem and made my own library for it that uses ctypes:
"""
< --- CTRL by [object Object] --- >
Only works on windows.
Some characters only work with a US standard keyboard.
Some parts may also only work in python 32-bit.
"""
#--- Setup ---#
from ctypes import *
from time import sleep
user32 = windll.user32
kernel32 = windll.kernel32
delay = 0.01
####################################
###---KEYBOARD CONTROL SECTION---###
####################################
#--- Key Code Variables ---#
class key:
cancel = 0x03
backspace = 0x08
tab = 0x09
enter = 0x0D
shift = 0x10
ctrl = 0x11
alt = 0x12
capslock = 0x14
esc = 0x1B
space = 0x20
pgup = 0x21
pgdown = 0x22
end = 0x23
home = 0x24
leftarrow = 0x26
uparrow = 0x26
rightarrow = 0x27
downarrow = 0x28
select = 0x29
print = 0x2A
execute = 0x2B
printscreen = 0x2C
insert = 0x2D
delete = 0x2E
help = 0x2F
num0 = 0x30
num1 = 0x31
num2 = 0x32
num3 = 0x33
num4 = 0x34
num5 = 0x35
num6 = 0x36
num7 = 0x37
num8 = 0x38
num9 = 0x39
a = 0x41
b = 0x42
c = 0x43
d = 0x44
e = 0x45
f = 0x46
g = 0x47
h = 0x48
i = 0x49
j = 0x4A
k = 0x4B
l = 0x4C
m = 0x4D
n = 0x4E
o = 0x4F
p = 0x50
q = 0x51
r = 0x52
s = 0x53
t = 0x54
u = 0x55
v = 0x56
w = 0x57
x = 0x58
y = 0x59
z = 0x5A
leftwin = 0x5B
rightwin = 0x5C
apps = 0x5D
sleep = 0x5F
numpad0 = 0x60
numpad1 = 0x61
numpad3 = 0x63
numpad4 = 0x64
numpad5 = 0x65
numpad6 = 0x66
numpad7 = 0x67
numpad8 = 0x68
numpad9 = 0x69
multiply = 0x6A
add = 0x6B
seperator = 0x6C
subtract = 0x6D
decimal = 0x6E
divide = 0x6F
F1 = 0x70
F2 = 0x71
F3 = 0x72
F4 = 0x73
F5 = 0x74
F6 = 0x75
F7 = 0x76
F8 = 0x77
F9 = 0x78
F10 = 0x79
F11 = 0x7A
F12 = 0x7B
F13 = 0x7C
F14 = 0x7D
F15 = 0x7E
F16 = 0x7F
F17 = 0x80
F19 = 0x82
F20 = 0x83
F21 = 0x84
F22 = 0x85
F23 = 0x86
F24 = 0x87
numlock = 0x90
scrolllock = 0x91
leftshift = 0xA0
rightshift = 0xA1
leftctrl = 0xA2
rightctrl = 0xA3
leftmenu = 0xA4
rightmenu = 0xA5
browserback = 0xA6
browserforward = 0xA7
browserrefresh = 0xA8
browserstop = 0xA9
browserfavories = 0xAB
browserhome = 0xAC
volumemute = 0xAD
volumedown = 0xAE
volumeup = 0xAF
nexttrack = 0xB0
prevoustrack = 0xB1
stopmedia = 0xB2
playpause = 0xB3
launchmail = 0xB4
selectmedia = 0xB5
launchapp1 = 0xB6
launchapp2 = 0xB7
semicolon = 0xBA
equals = 0xBB
comma = 0xBC
dash = 0xBD
period = 0xBE
slash = 0xBF
accent = 0xC0
openingsquarebracket = 0xDB
backslash = 0xDC
closingsquarebracket = 0xDD
quote = 0xDE
play = 0xFA
zoom = 0xFB
PA1 = 0xFD
clear = 0xFE
#--- Keyboard Control Functions ---#
# Category variables
letters = "qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM"
shiftsymbols = "~!##$%^&*()_+QWERTYUIOP{}|ASDFGHJKL:\"ZXCVBNM<>?"
# Presses and releases the key
def press(key):
user32.keybd_event(key, 0, 0, 0)
sleep(delay)
user32.keybd_event(key, 0, 2, 0)
sleep(delay)
# Holds a key
def hold(key):
user32.keybd_event(key, 0, 0, 0)
sleep(delay)
# Releases a key
def release(key):
user32.keybd_event(key, 0, 2, 0)
sleep(delay)
# Types out a string
def typestr(sentence):
for letter in sentence:
shift = letter in shiftsymbols
fixedletter = "space"
if letter == "`" or letter == "~":
fixedletter = "accent"
elif letter == "1" or letter == "!":
fixedletter = "num1"
elif letter == "2" or letter == "#":
fixedletter = "num2"
elif letter == "3" or letter == "#":
fixedletter = "num3"
elif letter == "4" or letter == "$":
fixedletter = "num4"
elif letter == "5" or letter == "%":
fixedletter = "num5"
elif letter == "6" or letter == "^":
fixedletter = "num6"
elif letter == "7" or letter == "&":
fixedletter = "num7"
elif letter == "8" or letter == "*":
fixedletter = "num8"
elif letter == "9" or letter == "(":
fixedletter = "num9"
elif letter == "0" or letter == ")":
fixedletter = "num0"
elif letter == "-" or letter == "_":
fixedletter = "dash"
elif letter == "=" or letter == "+":
fixedletter = "equals"
elif letter in letters:
fixedletter = letter.lower()
elif letter == "[" or letter == "{":
fixedletter = "openingsquarebracket"
elif letter == "]" or letter == "}":
fixedletter = "closingsquarebracket"
elif letter == "\\" or letter == "|":
fixedletter == "backslash"
elif letter == ";" or letter == ":":
fixedletter = "semicolon"
elif letter == "'" or letter == "\"":
fixedletter = "quote"
elif letter == "," or letter == "<":
fixedletter = "comma"
elif letter == "." or letter == ">":
fixedletter = "period"
elif letter == "/" or letter == "?":
fixedletter = "slash"
elif letter == "\n":
fixedletter = "enter"
keytopress = eval("key." + str(fixedletter))
if shift:
hold(key.shift)
press(keytopress)
release(key.shift)
else:
press(keytopress)
#--- Mouse Variables ---#
class mouse:
left = [0x0002, 0x0004]
right = [0x0008, 0x00010]
middle = [0x00020, 0x00040]
#--- Mouse Control Functions ---#
# Moves mouse to a position
def move(x, y):
user32.SetCursorPos(x, y)
# Presses and releases mouse
def click(button):
user32.mouse_event(button[0], 0, 0, 0, 0)
sleep(delay)
user32.mouse_event(button[1], 0, 0, 0, 0)
sleep(delay)
# Holds a mouse button
def holdclick(button):
user32.mouse_event(button[0], 0, 0, 0, 0)
sleep(delay)
# Releases a mouse button
def releaseclick(button):
user32.mouse_event(button[1])
sleep(delay)
macOS
Here is the more complete version of #Phylliida answer in form of class with code example:
#!/usr/bin/python
# Script simulating keyboard events in macOS.
# See: https://stackoverflow.com/q/13564851/55075
import sys
import time
from Quartz.CoreGraphics import CGEventCreateKeyboardEvent
from Quartz.CoreGraphics import CGEventPost
from Quartz.CoreGraphics import kCGHIDEventTap
#from Quartz.CoreGraphics import CFRelease # Python releases things automatically.
class Keyboard():
shiftChars = {
'~': '`',
'!': '1',
'#': '2',
'#': '3',
'$': '4',
'%': '5',
'^': '6',
'&': '7',
'*': '8',
'(': '9',
')': '0',
'_': '-',
'+': '=',
'{': '[',
'}': ']',
'|': '\\',
':': ';',
'"': '\'',
'<': ',',
'>': '.',
'?': '/'
}
keyCodeMap = {
'a' : 0x00,
's' : 0x01,
'd' : 0x02,
'f' : 0x03,
'h' : 0x04,
'g' : 0x05,
'z' : 0x06,
'x' : 0x07,
'c' : 0x08,
'v' : 0x09,
'b' : 0x0B,
'q' : 0x0C,
'w' : 0x0D,
'e' : 0x0E,
'r' : 0x0F,
'y' : 0x10,
't' : 0x11,
'1' : 0x12,
'2' : 0x13,
'3' : 0x14,
'4' : 0x15,
'6' : 0x16,
'5' : 0x17,
'=' : 0x18,
'9' : 0x19,
'7' : 0x1A,
'-' : 0x1B,
'8' : 0x1C,
'0' : 0x1D,
']' : 0x1E,
'o' : 0x1F,
'u' : 0x20,
'[' : 0x21,
'i' : 0x22,
'p' : 0x23,
'l' : 0x25,
'j' : 0x26,
'\'' : 0x27,
'k' : 0x28,
';' : 0x29,
'\\' : 0x2A,
',' : 0x2B,
'/' : 0x2C,
'n' : 0x2D,
'm' : 0x2E,
'.' : 0x2F,
'`' : 0x32,
'k.' : 0x41,
'k*' : 0x43,
'k+' : 0x45,
'kclear' : 0x47,
'k/' : 0x4B,
'k\n' : 0x4C,
'k-' : 0x4E,
'k=' : 0x51,
'k0' : 0x52,
'k1' : 0x53,
'k2' : 0x54,
'k3' : 0x55,
'k4' : 0x56,
'k5' : 0x57,
'k6' : 0x58,
'k7' : 0x59,
'k8' : 0x5B,
'k9' : 0x5C,
# keycodes for keys that are independent of keyboard layout
'\n' : 0x24,
'\t' : 0x30,
' ' : 0x31,
'del' : 0x33,
'delete' : 0x33,
'esc' : 0x35,
'escape' : 0x35,
'cmd' : 0x37,
'command' : 0x37,
'shift' : 0x38,
'caps lock' : 0x39,
'option' : 0x3A,
'ctrl' : 0x3B,
'control' : 0x3B,
'right shift' : 0x3C,
'rshift' : 0x3C,
'right option' : 0x3D,
'roption' : 0x3D,
'right control' : 0x3E,
'rcontrol' : 0x3E,
'fun' : 0x3F,
'function' : 0x3F,
'f17' : 0x40,
'volume up' : 0x48,
'volume down' : 0x49,
'mute' : 0x4A,
'f18' : 0x4F,
'f19' : 0x50,
'f20' : 0x5A,
'f5' : 0x60,
'f6' : 0x61,
'f7' : 0x62,
'f3' : 0x63,
'f8' : 0x64,
'f9' : 0x65,
'f11' : 0x67,
'f13' : 0x69,
'f16' : 0x6A,
'f14' : 0x6B,
'f10' : 0x6D,
'f12' : 0x6F,
'f15' : 0x71,
'help' : 0x72,
'home' : 0x73,
'pgup' : 0x74,
'page up' : 0x74,
'forward delete' : 0x75,
'f4' : 0x76,
'end' : 0x77,
'f2' : 0x78,
'page down' : 0x79,
'pgdn' : 0x79,
'f1' : 0x7A,
'left' : 0x7B,
'right' : 0x7C,
'down' : 0x7D,
'up' : 0x7E
}
# See: https://stackoverflow.com/q/3202629/55075
def toKeyCode(self, c):
shiftKey = False
# Letter
if c.isalpha():
if not c.islower():
shiftKey = True
c = c.lower()
if c in Keyboard.shiftChars:
shiftKey = True
c = Keyboard.shiftChars[c]
if c in Keyboard.keyCodeMap:
keyCode = Keyboard.keyCodeMap[c]
else:
keyCode = ord(c)
return keyCode, shiftKey
def KeyDown(self, k):
keyCode, shiftKey = self.toKeyCode(k)
time.sleep(0.0001)
if shiftKey:
CGEventPost(kCGHIDEventTap, CGEventCreateKeyboardEvent(None, 0x38, True))
time.sleep(0.0001)
CGEventPost(kCGHIDEventTap, CGEventCreateKeyboardEvent(None, keyCode, True))
time.sleep(0.0001)
if shiftKey:
CGEventPost(kCGHIDEventTap, CGEventCreateKeyboardEvent(None, 0x38, False))
time.sleep(0.0001)
def KeyUp(self, k):
keyCode, shiftKey = self.toKeyCode(k)
time.sleep(0.0001)
CGEventPost(kCGHIDEventTap, CGEventCreateKeyboardEvent(None, keyCode, False))
time.sleep(0.0001)
def KeyPress(self, k):
keyCode, shiftKey = self.toKeyCode(k)
time.sleep(0.0001)
if shiftKey:
CGEventPost(kCGHIDEventTap, CGEventCreateKeyboardEvent(None, 0x38, True))
time.sleep(0.0001)
CGEventPost(kCGHIDEventTap, CGEventCreateKeyboardEvent(None, keyCode, True))
time.sleep(0.0001)
CGEventPost(kCGHIDEventTap, CGEventCreateKeyboardEvent(None, keyCode, False))
time.sleep(0.0001)
if shiftKey:
CGEventPost(kCGHIDEventTap, CGEventCreateKeyboardEvent(None, 0x38, False))
time.sleep(0.0001)
def Type(self, text):
for key in text:
self.KeyDown(key)
self.KeyUp(key)
Here is the demo code using above class:
# DEMO
if __name__ == '__main__':
keyboard = Keyboard()
if sys.platform == "darwin":
keyboard.Type('Hello World!')
elif sys.platform == "win32":
print("Error: Platform not supported!")
which will simulate typing Hello World! text on the current window.
You can run above code as a shell script. Check the link to the keyboard.py file.
Windows only: You can either use Ironpython or a library that allows cPython to access the .NET frameworks on Windows. Then use the sendkeys class of .NET or the more general send to simulate a keystroke.
OS X only: Use PyObjC then use use CGEventCreateKeyboardEvent call.
Full disclosure: I have only done this on OS X with Python, but I have used .NET sendkeys (with C#) and that works great.
Every platform is going to have a different approach to being able to generate keyboard events. This is because they each need to make use of system libraries (and system extensions). For a cross platform solution, you would need to take each of these solutions and wrap then into a platform check to perform the proper approach.
For windows, you might be able to use the pywin32 extension. win32api.keybd_event
win32api.keybd_event
keybd_event(bVk, bScan, dwFlags, dwExtraInfo)
Simulate a keyboard event
Parameters
bVk : BYTE - Virtual-key code
bScan : BYTE - Hardware scan code
dwFlags=0 : DWORD - Flags specifying various function options
dwExtraInfo=0 : DWORD - Additional data associated with keystroke
You will need to investigate pywin32 for how to properly use it, as I have never used it.
For Python2.7(windows32) I installed only pywin32-223.
And I wrote simple python`s code:
import win32api
import time
import win32con
# simulate the pressing-DOWN "ARROW key" of 200 times
for i in range(200):
time.sleep(0.5)
win32api.keybd_event(0x28, 0,0,0)
time.sleep(.05)
win32api.keybd_event(0x28,0 ,win32con.KEYEVENTF_KEYUP ,0)
It can be checked if you run the code and immediately go to the notepad window (where the text already exists) and place the cursor on the top line.
regarding the recommended answer's code,
For my bot the recommended answer did not work. This is because I'm using Chrome which is requiring me to use KEYEVENTF_SCANCODE in my dwFlags.
To get his code to work I had to modify these code blocks:
class KEYBDINPUT(ctypes.Structure):
_fields_ = (("wVk", wintypes.WORD),
("wScan", wintypes.WORD),
("dwFlags", wintypes.DWORD),
("time", wintypes.DWORD),
("dwExtraInfo", wintypes.ULONG_PTR))
def __init__(self, *args, **kwds):
super(KEYBDINPUT, self).__init__(*args, **kwds)
# some programs use the scan code even if KEYEVENTF_SCANCODE
# isn't set in dwFflags, so attempt to map the correct code.
#if not self.dwFlags & KEYEVENTF_UNICODE:l
#self.wScan = user32.MapVirtualKeyExW(self.wVk,
#MAPVK_VK_TO_VSC, 0)
# ^MAKE SURE YOU COMMENT/REMOVE THIS CODE^
def PressKey(keyCode):
input = INPUT(type=INPUT_KEYBOARD,
ki=KEYBDINPUT(wScan=keyCode,
dwFlags=KEYEVENTF_SCANCODE))
user32.SendInput(1, ctypes.byref(input), ctypes.sizeof(input))
def ReleaseKey(keyCode):
input = INPUT(type=INPUT_KEYBOARD,
ki=KEYBDINPUT(wScan=keyCode,
dwFlags=KEYEVENTF_SCANCODE | KEYEVENTF_KEYUP))
user32.SendInput(1, ctypes.byref(input), ctypes.sizeof(input))
time.sleep(5) # sleep to open browser tab
PressKey(0x26) # press right arrow key
time.sleep(2) # hold for 2 seconds
ReleaseKey(0x26) # release right arrow key
I hope this helps someone's headache!
def keyboardevent():
keyboard.press_and_release('a')
keyboard.press_and_release('shift + b')
keyboardevent()
import ctypes
user32 = ctypes.WinDLL('user32')
class KBDIN(ctypes.Structure): _fields_ = (("wVk", ctypes.c_ushort),("dwFlags", ctypes.c_ulong),("dwExtraInfo", ctypes.c_ulonglong))
class INPUT(ctypes.Structure): _fields_ = (("type", ctypes.c_ulong),("ki", KBDIN),("padding", ctypes.c_ubyte * 8))
def Press(key_code): user32.SendInput(1, ctypes.byref(INPUT(type=1, ki=KBDIN(wVk=key_code))), 40)
def Release(key_code): user32.SendInput(1, ctypes.byref(INPUT(type=1, ki=KBDIN(wVk=key_code, dwFlags=2))), 40)
Virtual key codes

Categories

Resources