Sank6
Sank6

Reputation: 497

Keyboard Control Python

I want python to click Enter. However, I don't want to install any outside extensions. I want it to click enter without using them. I found many resources like win32 but I want to do it without using external resources. I don't mind which version of python it is on.

  1. Is this possible?
  2. If so, how?

I have looked on the web but couldn't find anything. Please, can someone help?

Thank you in advance.

Upvotes: 2

Views: 198

Answers (1)

RobertB
RobertB

Reputation: 1929

  1. Yes.
  2. It should definitely be possible using the ctypes library to talk directly to the Windows dlls

If you don't want to install a helper module that has done all this for you, then you pretty much have to do it old school by going after the really low level code.

In short, call the SendInput function of user32.dll by using ctypes.windll.user32.SendInput. Good luck with getting the parameters correct - I don't have the patience to figure it out for you.

Here are the docs for the user32.dll SendInput API

Here is a helpful resource for figuring out the data types. This is actually part of ctypes and can be imported by import ctypes.wintypes but I find it instructional to read the actual code.

So for example, to create a WORD with the value of the Virtual Key Code VK_RETURN, I think it would be

>>> ctypes.wintypes.WORD(0x0D)
c_ushort(13)

But there is a lot more you have to piece together from there. Just build up your parameters and call the function.

Last hint, use the examples in the second link for how to build "C" type structures. Then build one yourself using ctypes.Structure

Upvotes: 1

Related Questions