0

hello,
I'm rather new to python, and I'm trying to make a simple macro program, i have the mouse setup but duplicating the keyboard actions is a giving me a bit of trouble.
the website I'm using isn't for python but i found it for a reference:
http://www.codeproject.com/KB/system/keyboard.aspx
I found it on this forum:
http://www.dreamincode.net/forums/topic ... -event%3B/

The thing I'm confused at is that i have made a program that finds the scan code for the key, then it makes it into a hexadecimal, but the codeproject website has two columns of numbers and it requires the second "break" numbers. I dont know how to get those let-alone the 0xblah thing which is just a 0x in front of the break. The last thing about this I dont understand is the First parameter where it requires the virtual key... is there anyway i can leave this blank and just use the key code?

if someone could show me a simple code that takes in a scan code and puts out the according letter i would be able to take it from there.

2
Contributors
6
Replies
8
Views
7 Years
Discussion Span
Last Post by jcao219
0

sure, here's an example:

import ctypes

user32 = ctypes.windll.user32

while True:
    inputhex = raw_input("Please enter your desired key's code (HEX): ")
    keycode = int(inputhex, 16)

    #VOID keybd_event(BYTE bVk, BYTE bScan, DWORD dwFlags, PTR dwExtraInfo);

    user32.keybd_event(keycode,0,2,0)  #2 is the code for KEYDOWN
    user32.keybd_event(keycode,0,0,0)  #0 is the code for KEYUP

Keycodes here.

Edited by jcao219: n/a

0

Thanks for the help, but I can't get it to work (it won't type out a key). This is the code to get the hex of the key, that i am using.

import pythoncom, pyHook

def OnKeyboardEvent(event):
    print '%02x' % event.ScanCode
    print '---'
 
# return True to pass the event to other handlers
    return True
 
# create a hook manager
hm = pyHook.HookManager()
# watch for all mouse events
hm.KeyUp = OnKeyboardEvent
# set the hook
hm.HookKeyboard()
# wait forever
pythoncom.PumpMessages()
0

It will type out a key, but it types it so fast that you don't know that it is typed.

Just wrap it in a thread and make it execute 300ms later or so.

0

the problem isn't in your code it is how I obtain the input hex, my code will say that
a = 1e while your code is a = 41, anyway using pyhook that I could get it to output 41 for a?

0

the problem isn't in your code it is how I obtain the input hex, my code will say that
a = 1e while your code is a = 41, anyway using pyhook that I could get it to output 41 for a?

EDIT: I figured it out thank you for all of the help, i just needed to use hex(event.KeyID) for the inputhex. :D

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.