boppreh / mouse

Hook and simulate global mouse events in pure Python

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[help with code] high cpu usage

skhrlx opened this issue · comments

So, currently this is my code, i need to constantly check if the left mouse button is pressed, but, this code is taking about 25% of my CPU. How i can solve this problem?

import mouse

def main():

while True:
    if mouse.is_pressed(button='left'):
        print('Left mouse is down!')

if name == 'main':
main()

You can use callback:

import mouse

if __name__ == '__main__':
    mouse.on_button(lambda: print('Left mouse is down!'), buttons=mouse.LEFT, types=mouse.DOWN)
    input('Press <Enter> to exit.\n')

Thanks bro! It solved my problem.