0

I have a Python script that will simulate keyboard presses on my computer. The final function is of the form key(hexcode), where hexcode is the Hex Code of the key. I want to combine this with the "random" function, so I can type random strings in a program. For example, if I have the string
a = string.ascii_uppercase
and I want to write a random element of the string, all I need to do is write
print random.choice(a)

However, if I try to combine the type function with the random function, I get a whole lot of random errors. An example would be trying to type into Microsoft Word a random string of 6 capital letters. Any help is appreciated!

eddybob123
  • 121
  • 3
  • 1
    Please show your code. – AndyG Mar 19 '14 at 03:43
  • unless you need `hexcode` for some reason, you are making this a lot more complicated than it needs to be. If you want alphabetic choices you just need a string with 'abcedfg...z' in it. – Paul Mar 19 '14 at 03:47
  • @AndyG It is the same one that is in the first answer of this thread: http://stackoverflow.com/questions/13564851/generate-keyboard-events – eddybob123 Mar 19 '14 at 03:50
  • @Paul I don't think you understand. The module that is imported requires the hexcode, but that is beside the point. If you have any way to type random strings, I'd like to hear it. – eddybob123 Mar 19 '14 at 03:56
  • what do you mean by 'type'? send to stdin? of which process? generate keyboard event? what app should process it? and the most important question, what systems should it run on? – m.wasowski Mar 19 '14 at 04:02
  • 1
    @eddybob123 I probably don't understand. Like AndyG asked, posting more of the code would probably help people help you. – Paul Mar 19 '14 at 04:03
  • @ m.wasowski type as in simulate keystrokes – eddybob123 Mar 19 '14 at 04:05
  • if you have problem only with converting char to hexcode, see http://stackoverflow.com/questions/8088375/how-do-i-convert-a-single-character-into-its-hex-ascii-value-in-python – m.wasowski Mar 19 '14 at 04:05
  • @ Paul the code is in the link I provided in my first comment of this thread. – eddybob123 Mar 19 '14 at 04:06
  • @eddybob123 any code needed for people to answer the question should be in the question itself, not in a comment - and anyway, that linked code is not _your_ code. That code doesn't give random errors, but your code does. Show us what you're doing that produces these errors, and it will help people understand what you need to do differently. – David Z Mar 19 '14 at 05:44

1 Answers1

0

Assuming that hexcode is a plain 2-digit "number" with digits from 0 to F, this should work nicely:

def rand_hexcode():
    return '0123456789ABCDEF'[random.randint(0, 15)] + '0123456789ABCDEF'[random.randint(0, 15)]

If you only want ASCII range hex codes (0-127 decimal, or 0x00 to 0x7F hex) then use a truncated string for the first digit:

def asciirand_hexcode():
    return '01234567'[random.randint(0, 8)] + '0123456789ABCDEF'[random.randint(0, 15)]
Pi Marillion
  • 4,465
  • 1
  • 19
  • 20