3

is there any way to make keyboard input from my program. suppose my program reeives "1" from a socket. how this data can be converted to a real time keyboard hit. means when "1" would receive , the computer would think that i pressed "1" .

2

2 Answers 2

4

In Windows, you can use pywin32 to make a keypress. See this previous answer for a code example.

In Linux, this previous answer has you covered, using xsendkey or xsendkeycode

And for Macs, another previous answer using PyQt or wxPython.

Sign up to request clarification or add additional context in comments.

3 Comments

if i want to hold a key for 1second then , what method should i use? for this purpose
Which method are you using? Or what OS?
#python 2.7 in win7. sendkeys 0.3 import win32com.client import time import SendKeys import os from ctypes import * shell = win32com.client.Dispatch("WScript.Shell") os.startfile("E:/Projects/server-client socket/dist/test.exe") time.sleep( 3 ) shell.SendKeys('h') shell.SendKeys('4') # i want to hold the 4 key for 1 sec
2

Have a look at this https://github.com/SavinaRoja/PyUserInput its cross-platform control for mouse and keyboard in python

Keyboard control works on X11(linux) and Windows systems. But no mac support as of now.

from pykeyboard import PyKeyboard
k = PyKeyboard()
k.tap_key(k.numpad_keys[1], n=1)  # Tap 1 on the numpad once.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.