P
P
PosikGG2021-06-10 11:13:55
Python
PosikGG, 2021-06-10 11:13:55

Why does the game not see keystrokes?

The bottom line is that I wrote a program for automation. The game has a simulator, but that's not the point.
The whole catch is that the program works, when I open the notepad, it presses the right keys, and when I open the game, it does not seem to register these keystrokes. How to fix it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
PosikGG, 2021-06-10
@PosikGG

The program code is here:

import cv2
import difflib
import time
from PIL import ImageGrab
from ahk import AHK

user=True
ahk = AHK()

def CalcImageHash(FileName):
    image = cv2.imread(FileName) 
    resized = cv2.resize(image, (8,8), interpolation = cv2.INTER_AREA) 
    gray_image = cv2.cvtColor(resized, cv2.COLOR_BGR2GRAY) 
    avg=gray_image.mean() 
    ret, threshold_image = cv2.threshold(gray_image, avg, 255, 0) 
    
    
    _hash=""
    for x in range(8):
        for y in range(8):
            val=threshold_image[x,y]
            if val==255:
                _hash=_hash+"1"
            else:
                _hash=_hash+"0"
            
    return _hash

def CompareHash(hash1,hash2):
    l=len(hash1)
    i=0
    count=0
    while i<l:
        if hash1[i]!=hash2[i]:
            count=count+1
        i=i+1
    return count
        
while user:
    time.sleep(2)
    
    box1=(1049,884,1253,1016)
    ImageGrab.grab(box1).save('box1.png')
    
    hash1=CalcImageHash("box1.png")
    hash2=CalcImageHash("q.png")
    hash3=CalcImageHash("w.png")
    hash4=CalcImageHash("e.png")
    hash5=CalcImageHash("a.png")
    hash6=CalcImageHash("s.png")
    
    CompareHash(hash1, hash2)
    print(CompareHash(hash1, hash2))
    
    if CompareHash(hash1, hash2) <=4:
        print("Q")
        ahk.key_press('q')
    else:
        CompareHash(hash1, hash3)
        if CompareHash(hash1, hash3) <=4:
            print("W")
            ahk.key_press('w')   
        else:
            CompareHash(hash1, hash4)
            if CompareHash(hash1, hash4) <=4:
                print("E")
                ahk.key_press('e')     
            else:
                CompareHash(hash1, hash5)
                if CompareHash(hash1, hash5) <=4:
                    print("A")
                    ahk.key_press('a')
                else:
                    CompareHash(hash1, hash6)
                    if CompareHash(hash1, hash6) <=4:
                        print("S")
                        ahk.key_press('s')
                    else:
                        print("D")
                        ahk.key_press('d')

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question