bode135 / pydamo

python写游戏外挂的工具箱
271 stars 79 forks source link

按键不同步 #6

Open gflkdns opened 4 years ago

gflkdns commented 4 years ago

例如代码是

        k.KeyDown(VK.a)
        k.KeyUp(VK.a)

        k.KeyDown(VK.b)
        k.KeyUp(VK.b)

        k.KeyDown(VK.c)
        k.KeyUp(VK.c)

打开记事本 按键顺序为 acb bbaa ccc 这种

gflkdns commented 4 years ago
from bd_time import VK
from pydamo_0 import Time, DM, Mouse

dm = DM()
ms = Mouse(dm)
tt = Time()

print(ms.position)

while True:
    tt.sleep(1)
    dm.KeyDown(VK.a)
    dm.KeyUp(VK.a)

    tt.sleep(1)
    dm.KeyDown(VK.b)
    dm.KeyUp(VK.b)

    tt.sleep(1)
    dm.KeyDown(VK.c)
    dm.KeyUp(VK.c)

    tt.sleep(1)
    dm.KeyDown(VK.d)
    dm.KeyUp(VK.d)

    # 输出 ababcdacdabca
bode135 commented 4 years ago

keydown和keyup之间加sleep

发自我的iPhone

------------------ 原始邮件 ------------------ 发件人: miqt <notifications@github.com> 发送时间: 2020年11月11日 15:32 收件人: bode135/pydamo <pydamo@noreply.github.com> 抄送: Subscribed <subscribed@noreply.github.com> 主题: 回复:[bode135/pydamo] 按键不同步 (#6)

from bd_time import VK from pydamo_0 import Time, DM, Mouse

dm = DM() ms = Mouse(dm) tt = Time()

print(ms.position)

while True: tt.sleep(1) dm.KeyDown(VK.a) dm.KeyUp(VK.a) tt.sleep(1) dm.KeyDown(VK.b) dm.KeyUp(VK.b) tt.sleep(1) dm.KeyDown(VK.c) dm.KeyUp(VK.c) tt.sleep(1) dm.KeyDown(VK.d) dm.KeyUp(VK.d) # 输出 ababcdacdabca
— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub, or unsubscribe.

gflkdns commented 4 years ago

加了还是一样,这两个方法应该是不同步的

另外我实在不理解这个:

from bd_time import VK
from pydamo_0 import Time, DM, Mouse

dm = DM()
ms = Mouse(dm)
tt = Time()

def click(key):
    dm.KeyDown(key)
    tt.sleep(0.2)
    dm.KeyUp(key)

click(VK.c)
click(VK.d)
click(VK.a)
click(VK.b)
click(VK.e)

tt.sleep(1)

# cde

ab 位置没了。 ab 位置不管写什么都是不能触发 😟

bode135 commented 4 years ago
from bd_time import tt, vk
from pydamo_0 import Time, DM, Mouse, Key

dm = DM()
ms = Mouse(dm)
kk = Key(dm)
tt = Time()

keys = [vk.c, vk.d, vk.a, vk.b, vk.e]
tmp = 0.2

tt.sleep(1)
for key in keys:
    tt.sleep(tmp)
    kk.dp(key, tmp)
    tt.sleep(tmp)

# 输出:
# cdabe

我用python console调试的时候没有误差误差比较小, 换成你的click()函数也是一样的输出, 不过换成直接运行文件就出现较大的遗漏现象了. 建议换VirtualKey, 这个在多个按键输入的时候没有误差.

from VirtualKey import down_up, vk
from bd_time import tt

# down_up(vk.a)

# keys = [ vk.a, vk.b, vk.a, vk.b, vk.a, vk.b]
keys = [vk.c, vk.d, vk.a, vk.b, vk.e]
tmp = 0.2

tt.sleep(1)
for key in keys:
    print('-' * 50)
    print('press :', chr(key))

    down_up(key, tmp)

    print('up :', chr(key))
# 输出: cdabe
bode135 commented 4 years ago

....发现了一个折中的办法...好像漏掉的字符是有规律的, 也许可以摸索规律判断是否会被遗漏?... 另外以前没出现这个BUG, 可能是最近新版大漠插件的问题(最近大漠插件的LOL后台操作功能也失效了). 建议先用Ctypes的VirtualKey将就一下吧. 以后解决, 最近有点忙, 得刷算法题准备面试了...=- =

from bd_time import tt, vk
from pydamo_0 import Time, DM, Mouse, Key

dm = DM()
ms = Mouse(dm)
kk = Key(dm)
tt = Time()

keys = list(range(vk.a, vk.z+1))
tmp = 0.2

tt.sleep(1)
for key in keys:
    print('-' * 50)
    print('press :', chr(key))

    kk.dp(key, tmp)

    print('up :', chr(key))

# 输出:
# abefghiklmnoqtwxyz
# abefghiklmnoqtwxyz
# abefghiklmnoqtwxyz
# abefghiklmnoqtwxyz
# 可以发现输出都是一样的, 遗漏的位置都一样. 也许这是个突破口...
bode135 commented 4 years ago

解决版本:

from VirtualKey import down_up, vk
from bd_time import tt

keys = list(range(vk.a, vk.z+1))
tmp = 0.2

tt.sleep(1)
for key in keys:
    print('-' * 50)
    print('press :', chr(key))

    down_up(key, tmp)

    print('up :', chr(key))
# abcdefghijklmnopqrstuvwxyz
gflkdns commented 4 years ago

多谢,可能是dm的bug吧,先用 Ctypes 顶上了,Ctypes 没这个问题,就是不知道会不会被LOL检查屏蔽,不是专业python的,你的代码我明天再试试。

bode135 commented 3 years ago

.我这昨天写了LOL卡装备CD的脚本,发现Ctypes的已经被屏蔽了? 后面用dm.dll实现的= =... 你那儿测试的怎么样了呢

gflkdns commented 3 years ago

.我这昨天写了LOL卡装备CD的脚本,发现Ctypes的已经被屏蔽了? 后面用dm.dll实现的= =... 你那儿测试的怎么样了呢

我这很好使啊,是不是你没给管理员权限,Ctypes按键同步而且耗时我测了下很低,我现在天天用我写的走A玩天使呢,就是攻速还不能获取,买装备得手动设置攻速

bode135 commented 3 years ago

emmm... 回头试下. 不过我当时还测试过ctypes好像不支持鼠标

gflkdns commented 3 years ago
from ctypes import POINTER, c_ulong, Structure, c_ushort, c_short, c_long, byref, windll, pointer, sizeof, Union

PUL = POINTER(c_ulong)

class KeyBdInput(Structure):
    _fields_ = [("wVk", c_ushort),
                ("wScan", c_ushort),
                ("dwFlags", c_ulong),
                ("time", c_ulong),
                ("dwExtraInfo", PUL)]

class HardwareInput(Structure):
    _fields_ = [("uMsg", c_ulong),
                ("wParamL", c_short),
                ("wParamH", c_ushort)]

class MouseInput(Structure):
    _fields_ = [("dx", c_long),
                ("dy", c_long),
                ("mouseData", c_ulong),
                ("dwFlags", c_ulong),
                ("time", c_ulong),
                ("dwExtraInfo", PUL)]

class Input_I(Union):
    _fields_ = [("ki", KeyBdInput),
                ("mi", MouseInput),
                ("hi", HardwareInput)]

class Input(Structure):
    _fields_ = [("type", c_ulong),
                ("ii", Input_I)]

class POINT(Structure):
    _fields_ = [("x", c_ulong),
                ("y", c_ulong)]

def get_mpos():
    orig = POINT()
    windll.user32.GetCursorPos(byref(orig))
    return int(orig.x), int(orig.y)

def set_mpos(pos):
    x, y = pos
    windll.user32.SetCursorPos(x, y)

def move_click(pos, move_back=False):
    origx, origy = get_mpos()
    set_mpos(pos)
    FInputs = Input * 2
    extra = c_ulong(0)
    ii_ = Input_I()
    ii_.mi = MouseInput(0, 0, 0, 2, 0, pointer(extra))
    ii2_ = Input_I()
    ii2_.mi = MouseInput(0, 0, 0, 4, 0, pointer(extra))
    x = FInputs((0, ii_), (0, ii2_))
    windll.user32.SendInput(2, pointer(x), sizeof(x[0]))
    if move_back:
        set_mpos((origx, origy))
        return origx, origy

def sendkey(scancode, pressed):
    FInputs = Input * 1
    extra = c_ulong(0)
    ii_ = Input_I()
    flag = 0x8
    ii_.ki = KeyBdInput(0, 0, flag, 0, pointer(extra))
    InputBox = FInputs((1, ii_))
    if scancode is None:
        return
    InputBox[0].ii.ki.wScan = scancode
    InputBox[0].ii.ki.dwFlags = 0x8

    if not (pressed):
        InputBox[0].ii.ki.dwFlags |= 0x2

    windll.user32.SendInput(1, pointer(InputBox), sizeof(InputBox[0]))

这是我用的模拟点击和按键的代码

bode135 commented 3 years ago

不太懂驱动编程, 只能复制粘贴了... 找到键盘扫描码后, 按键能用了, 但鼠标还是用不了=- = 你那能在LOL里左键和右键鼠标嘛

bode135 commented 3 years ago

另外这个PressKey什么意思呢...

def sendkey(scancode, pressed):
    FInputs = Input * 1
    extra = c_ulong(0)
    ii_ = Input_I()
    flag = 0x8
    ii_.ki = KeyBdInput(0, 0, flag, 0, pointer(extra))
    InputBox = FInputs((1, ii_))
    if scancode is None:
        return
    InputBox[0].ii.ki.wScan = scancode
    InputBox[0].ii.ki.dwFlags = 0x8

    if not (pressed):                                                # ---------------- 这个是啥意思...?
        InputBox[0].ii.ki.dwFlags |= 0x2

    windll.user32.SendInput(1, pointer(InputBox), sizeof(InputBox[0]))
gflkdns commented 3 years ago

PressKey 1是按下,0是拿起,另外鼠标操作我试了下没开游戏可以,鼠标在游戏中是否生效我还没试过。

gflkdns commented 3 years ago

https://github.com/miqt/LOLGameTools/blob/master/ZA_Max.py 你或许可以参考下

gflkdns commented 3 years ago

或者把游戏设置成 无边框&窗口模式 试试呢

bode135 commented 3 years ago

一起努力~ 我准备重写个挂机小脚本了... 以前写的猫咪挂机脚本写得太烂了=- =

bode135 commented 3 years ago

用win32gui里的FindWindow找不到lol开始客户端的句柄=- = ... 头疼

gflkdns commented 3 years ago

这个我也没经验😄

bode135 commented 3 years ago

哈  没事 我在看设计模式的时候想到怎么解决大漠按键偶尔失灵的问题了~ 建立一个观察者,hook系统层的按键消息,如果按键失灵就再次按下,一直到成功为止🤣 回头打个补丁哈哈哈

发自我的iPhone

------------------ 原始邮件 ------------------ 发件人: miqt <notifications@github.com> 发送时间: 2020年12月1日 20:09 收件人: bode135/pydamo <pydamo@noreply.github.com> 抄送: bode <2248270222@qq.com>, State change <state_change@noreply.github.com> 主题: 回复:[bode135/pydamo] 按键不同步 (#6)

这个我也没经验😄

— You are receiving this because you modified the open/close state. Reply to this email directly, view it on GitHub, or unsubscribe.

gflkdns commented 3 years ago

那样你得设置个超时时间吧,延迟太高了

Giles2000 commented 3 years ago

用win32gui里的FindWindow找不到lol开始客户端的句柄=- = ... 头疼

你可以搜索下大漠插件绑定测试工具. 提供挨个前后台鼠标键盘测试的功能 还不错