15
2024
11

python编程学习-模拟键盘按键

学习python编程,在新东方少儿编程官网的自由创作平台做的一些作品。

可以在线运行,效果如下(在新页面中打开链接):

代码如下(用到平台自定义的xdf库):

from xdf import *

class MyKeyBoard:
    def __init__(self, x, y, w):
        hw = w / 2
        hhw = w * 0.75
        self.arr = [
            box(x, y - w, w, w, "white", "red"),
            text("↑", x + hw, y - w + hhw, w, "center", "red"),
            box(x, y, w, w, "white", "red"),
            text("↓", x + hw, y + hhw, w, "center", "red"),
            box(x - w, y, w, w, "white", "red"),
            text("←", x - hw, y + hhw, w, "center", "red"),
            box(x + w, y, w, w, "white", "red"),
            text("→", x + w + hw, y + hhw, w, "center", "red"),
        ]
        self.arr[0].tap=self.clickUp
        self.arr[2].tap=self.clickDown
        self.arr[4].tap=self.clickLeft
        self.arr[6].tap=self.clickRight
        delay(self.loop, 100)
        
    def loop(self):
        for i in range(len(self.arr)):
            self.arr[i].front()
        delay(self.loop, 100)
    
    def onKeydown(self, keyCode, key):
        try:
            keydown(keyCode, key)
        except e:
            pass
    def clickUp(self):
        self.onKeydown(38, "ArrowLeft")
    def clickDown(self):
        self.onKeydown(40, "ArrowDown")
    def clickLeft(self):
        self.onKeydown(37, "ArrowLeft")
    def clickRight(self):
        self.onKeydown(39, "ArrowRight")
MyKeyBoard(768-200, 1024-100, 100)
def keydown(keyCode, key):
    print(keyCode, key)


也可以下载代码,本地python环境运行(用pygame封装了xdf库)。



« 上一篇下一篇 »

相关文章:

python编程学习-汉诺塔  (2024-11-15 11:36:16)

python编程学习-显示帧频  (2024-11-15 11:35:26)

python编程学习-收不完的西瓜  (2024-11-15 11:35:0)

python编程学习-推箱子  (2024-11-15 11:34:1)

python编程学习-拼图  (2024-11-15 11:33:25)

python编程学习-扫雷  (2024-11-15 11:32:58)

python编程学习-小学数学加减分解题  (2024-11-15 11:32:29)

python编程学习-对角棋  (2024-11-15 11:32:8)

python编程学习-密室逃生  (2024-11-15 11:31:35)

python编程学习-围住神经猫  (2024-11-15 11:31:6)

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。