Source

phonelikeinputmethodonpc / wd_key_mapping.pyw

Full commit
# -*- coding: utf-8 -*-
shengMu=['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','zh','ch','sh']
yunMu= ['a','o','e','i','u','ai','ei','ui','ao','ou','iu','ie','ve','er','an','en','in','un','vn','ang','eng','ing','ong']
from pinyin import pinYin
import random
keyDic={"2":"abc","3":"def","4":"ghi","5":"jkl","6":"mno","7":"pqrs","8":"tuv","9":"wxyz"}
#for (key,value) in keyDic.items():
	#print key,value
running=0
def py(inputKey):
	inputKey=list(inputKey)
	tempList=[]
	word=[]
	output=[]
	for key in inputKey:
		tempList.append(list(keyDic[key]))
	pinYinNum=1
	for key in inputKey:
		pinYinNum*=len(keyDic[key])

	while 1:
		word=[]
		for j in range(len(inputKey)):
			try:
				w=random.choice(tempList[j])
				word.append(w)
				#tempList[j].remove(w)
			except:
				pass
		word="".join(word)
		if word not in output:
			output.append(word)
		word=[]
		if len(output)==pinYinNum:
			break
	return [word for word in output if word  in pinYin]


import win32api
import win32con

# A 65 0 48
def up(key):
	if type(key)==type(''):
		win32api.keybd_event(ord(key.upper()),0,win32con.KEYEVENTF_KEYUP,0)
	elif type(key)==type(1):
		win32api.keybd_event(key,0,win32con.KEYEVENTF_KEYUP,0)
def down(key):
	if type(key)==type(''):
		win32api.keybd_event(ord(key.upper()),0,0,0)
	elif type(key)==type(1):
		win32api.keybd_event(key,0,0,0)
def press(key):
	if type(key)==type(''):
		win32api.keybd_event(ord(key.upper()),0,0,0)
		win32api.keybd_event(ord(key.upper()),0,win32con.KEYEVENTF_KEYUP,0)
	elif type(key)==type(1):
		win32api.keybd_event(key,0,0,0)
		win32api.keybd_event(key,0,win32con.KEYEVENTF_KEYUP,0)
def out(keys):
	# outputChar.set('')
	# outputEntry.focus_set()
	for char in keys:
		press(char)
def backToInputScreen():
	'''���ص�ԭ�������'''
	tab=9
	alt=18
	down(alt)
	down(tab)
	up(tab)
	up(alt)
def getpinyin(x=0):
	# global pybt
	global pinyin
	# for bt in pybt:
		# bt.unpcak()
	pinyin=py(inputChar.get())
	# for p in pinyin:
		# exec("""bt=Button(root,text="%s",command=lambda : out("%s"))\nbt.pack()\npybt.append(bt)"""%(p,p))
	pinyinChar.set(','.join([p+str(pinyin.index(p)+1) for p in pinyin]))
	inputIndexChar.set('')
	inputIndexEntry.focus_set()

def putpinyin(x=0):
	global pinyin
	# outputEntry.focus_set()
	backToInputScreen()
	out(pinyin[int(inputIndexChar.get())-1])
	inputChar.set('')
	inputEntry.focus_set()


pinyin=[]
# win32api.keybd_event(17,0,0,0)  #ctrl�����17
# win32api.keybd_event(86,0,0,0)  #v�����86
# win32api.keybd_event(86,0,win32con.KEYEVENTF_KEYUP,0) #�ͷŰ���
# win32api.keybd_event(17,0,win32con.KEYEVENTF_KEYUP,0)
from Tkinter import *
root = Tk()
# pybt=[]
inputChar = StringVar()
inputEntry = Entry(root,textvariable = inputChar)
inputEntry.bind("<Return>",getpinyin)
inputEntry.pack()
inputEntry.focus_set()

inputIndexChar = StringVar()
inputIndexEntry = Entry(root,textvariable = inputIndexChar)
inputIndexEntry.bind("<Return>",putpinyin)
inputIndexEntry.pack()


# outputChar = StringVar()
# outputEntry = Entry(root,textvariable = outputChar)
# outputEntry.bind("<Return>",lambda x=0: inputEntry.focus_set())
# outputEntry.pack()

pinyinChar = StringVar()
pinyinEntry = Entry(root,textvariable = pinyinChar)
pinyinEntry.pack()

img=PhotoImage(file="keymap.gif")
label=Label(root,image=img)
label.pack()
# SENDBUTTON=Button(root,text='SEND',command=putpinyin)
# SENDBUTTON.pack()
root.mainloop()