ethanhs / pyhooked

Pure Python hotkey hook, with thanks to pyHook and pyhk
155 stars 28 forks source link

WindowsError: exception: access violation reading 0x00000004 #4

Closed simon2x closed 8 years ago

simon2x commented 9 years ago

Hi, I receive a WindowsError exception when trying to use this module:

Traceback (most recent call last): File "C:\Applications\scroll\scroll.py", line 182, in TaskBarIcon().Show() File "C:\Applications\scroll\scroll.py", line 130, in init hk.listen() File "C:\Applications\scroll\pyhookedinit.py", line 199, in listen self.listener() File "C:\Applications\scroll\pyhookedinit.py", line 194, in listener msg = windll.user32.GetMessageW(None, 0, 0,0) WindowsError: exception: access violation reading 0x00000004

Example usage:

import wx
from pyhooked import hook

class TaskBarIcon(wx.TaskBarIcon):

    def __init__(self):
        wx.TaskBarIcon.__init__(self, parent=None)

        hk=hook() 
        hk.Hotkey(["LCtrl","8"],self.OnStart) # hotkey 0
        hk.Hotkey(["LAlt","7"],self.OnStop) #hotkey 1
        hk.listen()

    def OnStart(self):
        pass
    def OnStop(self):
        pass
ethanhs commented 9 years ago

Well, your code isn't correctly formatted, so that makes things a little more difficult to understand, but based on the error message, the problem is with memory access, I will look into a fix. Also, your line numbers do not match the one in the repository, any idea why?

simon2x commented 9 years ago

Apologies, updated the code (Hopefully this is clearer). It is a simple wx application, which I intend to show wx.Frame when hotkey is pressed.

Line numbers should now be correct as I changed the dictionary self.keylist to multi-line originally

ethanhs commented 9 years ago

Hmm. I am not sure what is causing this.

ethanhs commented 8 years ago

So your example code is not reproducible/usable, as it would never run __init__ so that does not give you the error you are getting. Could you post some code of the actual code you used? I am restarting on this library, and plan on doing better on supporting it.

simon2x commented 8 years ago

ok, since I posted this issue I have switched to python 3.5 and wxpython Phoenix build (as wx does not yet officially support python 3.5).

and since then I have stopped working on the project which requires a global hotkey module
I think it is best avoided trying to use pyhooked in the way I described in the first post.

The way I worked around this was to use threading so MainLoop() doesn't lock the script and the hotkeys can be defined.

here is some example code:

import sys
import time
import wx
import threading
from pyhooked import hook

StopApp = False

class Frame(wx.Frame):

    def __init__(self):
        wx.Frame.__init__(self, 
                        parent=None,
                        size=(300,300),
                        title="pyhooked example")   

        panel = wx.Panel(self)
        sizer = wx.BoxSizer(wx.HORIZONTAL)
        self.button = wx.Button(panel, label="application running")
        sizer.Add(self.button, 1, wx.ALL|wx.EXPAND)
        panel.SetSizer(sizer)

        #timer check if StopApp has changed
        self.timer = wx.Timer()
        self.timer.Bind(wx.EVT_TIMER, self.evt_timer, self.timer)

        self.timer.Start(200)

    def evt_timer(self, msg): 
        if StopApp is True:
            self.button.SetLabel("Hotkey pressed. Quitting application")            
            for x in range(0,3):
                print(x)
                time.sleep(1)
            self.Destroy()
            sys.exit()            
        print(StopApp)

def start_app():           
    app = wx.App()                   
    Frame().Show()
    app.MainLoop()

def stop_app():  
    global StopApp
    StopApp = True

if __name__ == '__main__':
    thread = threading.Thread(target=start_app)
    thread.start()

    hk=hook() 
    hk.Hotkey(["LCtrl","8"], stop_app)    
    hk.listen()

The wx Frame is created and a thread safe wx.timer is started (self.timer) which checks the global variable StopApp every 200ms.

The hotkey changes StopApp to True. If true, the button label is changed and the application is closed after 3 seconds.

This seems to work with python 2.7.

I have tried this in python 3.5 but it doesn't seem to get past the mainloop.

ethanhs commented 8 years ago

Thanks so much for the info, I will take a look.

simon2x commented 8 years ago

closing this issue. The WindowsError: exception: access violation reading 0x00000004 as I think it is the incorrect usage of pyhooked in an init of GUI class.

And the GUI example above does show but the hotkey does not work. A separate issue raised.