flash钢琴自动弹奏之Python实现

linux系统下没有WSH可用,需要自己实现按键sendkey?python已有实现,virtkey拿来用即可。

不过需要注意的是,按键长度与延时是并行关系。

简单实现:

#!/usr/bin/python

import virtkey
import time
import threading
import sys

speed=0.25

def play(s):
	tid=0
	threading.Thread(target=playb,args=(tid,s)).start()

def playb(tid,s):
	v=virtkey.virtkey()
	ssc=list(s)
	print(ssc)
	for c in ssc:
		v.press_unicode(ord(c))
	time.sleep(speed)
	for c in ssc:
		v.release_unicode(ord(c))


def music(m):
	m=m.strip()
	codes=list(m)
	oncekeys=""
	state=""
	for code in codes:
		if code=="(":
			state="once"
			oncekeys=""
			continue
		if code==")":
			state=""
			play(oncekeys)
			time.sleep(speed)
			continue
		if code=="-":
			time.sleep(speed)
			continue
		if code=="" or code==" " or code=="\r" or code=="\n":
			continue
		if state=="once":
			oncekeys+=code
			continue
		else:
			play(code)
			time.sleep(speed)
			

time.sleep(3)
if len(sys.argv)==1:
	music("""
TU(VF)JM(OU)VX(UE)ILNQS(TD)HK(MS)TV(SC)GJLPQ(RI)MQ(RI)KV(QH)LOLVWU(TG)4(TN)(U4)V(UC)GJLTU(VF)JM(OU)VX(EU)ILNQS(DT)HK(SM)TV(SC)GJLPQ(IR)MQ(RI)KV(VC)GJMVWU(TG)4(TN)(40)(TF)JMOVW(XH)LO(WP)(XQ)(ZO)NM(WE)ILMNO(PS)Q(ZD)HK(XL)(XM)O(PW)(RX)(XC)GJ5NJ(MT)(NU)(VF)JM(NU)(OV)P(QX)L(ZE)IL(MW)(NW)O(PV)(QW)(YD)HK(LY)(XM)(UO)OPR(XC)GJ5NJ(MV)(NW)(XH)LO(XP)(XQ)(ZL)MO(WE)ILMNO(PS)Q(ZD)HK(LZ)(XM)(XO)P(RW)(XC)GJ5NJ(OT)(NU)(VF)JM(VN)(VO)P(XQ)M(ZE)IL(ZM)(ZN)O(ZP)(ZQ)(VD)H(WK)(VL)(UC)(TG)J(50)(TF)JMQT
""")
else:
	music(open(sys.argv[1],"r").read())


默认播放天空之城,可加1参数指定文件


你可能感兴趣的:(flash钢琴自动弹奏之Python实现)