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" .

arifcse10
  • 133
  • 2
  • 3
  • 12

2 Answers2

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.

Community
  • 1
  • 1
Wehrdo
  • 560
  • 6
  • 12
  • if i want to hold a key for 1second then , what method should i use? for this purpose – arifcse10 Nov 06 '12 at 18:58
  • Which method are you using? Or what OS? – Wehrdo Nov 07 '12 at 03:33
  • #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 – arifcse10 Nov 07 '12 at 11:51
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.
naren
  • 14,611
  • 5
  • 38
  • 45