예제 #1
0
    def handler(self, event):

        try:
            activeApps = self.workspace.runningApplications()
            for app in activeApps:
                if app.isActive():
                    if app.localizedName() != self.currentApp:
                        self.currentApp = app.localizedName()
                        options = kCGWindowListOptionOnScreenOnly 
                        windowList = CGWindowListCopyWindowInfo(options, kCGNullWindowID)

                        for window in windowList:
                            if window['kCGWindowOwnerName'] == self.currentApp:
                                geom = window['kCGWindowBounds'] 
                                self.screen_hook( event=event,
                                                name = window['kCGWindowName'],
                                                owner = window['kCGWindowOwnerName'],
                                                x = geom['X'], 
                                                y = geom['Y'], 
                                                w = geom['Width'], 
                                                h = geom['Height'])
                                break
                    break

            loc = NSEvent.mouseLocation()

            # mouse clicky buttons
            if event.type() in ( NSLeftMouseDown, NSRightMouseDown, NSLeftMouseUp, NSRightMouseUp):
                self.mouse_button_hook(event=event, x=loc.x, y=loc.y)

            # mouse scrolly buttons 
            elif event.type() == NSScrollWheel:
                if event.deltaY() > 0 and event.deltaY() < 0:
                    self.mouse_button_hook(event=event, x=loc.x, y=loc.y)
                if event.deltaX() > 0 and event.deltaX() < 0:
                    self.mouse_button_hook(event=event, x=loc.x, y=loc.y)

            # keys down
            elif event.type() in ( NSKeyDown, NSKeyUp ):

                flags = event.modifierFlags()
                modifiers = [] # OS X api doesn't care it if is left or right
                if (flags & NSControlKeyMask):
                    modifiers.append('CONTROL')
                if (flags & NSAlternateKeyMask):
                    modifiers.append('ALTERNATE')
                if (flags & NSCommandKeyMask):
                    modifiers.append('COMMAND')

                self.key_hook(event=event, key=event.keyCode(), char=keycode.tostring( event.keyCode() ), mods=modifiers, is_repeat=event.isARepeat())

            # Mouse moved
            elif event.type() == NSMouseMoved:
                self.mouse_move_hook(event=event, x=loc.x, y=loc.y)
            else:
                pass

        except ( KeyboardInterrupt ) as e:
            print 'handler', e
            AppHelper.stopEventLoop()
예제 #2
0
파일: TBot.py 프로젝트: mmwang/TBot
def handler(event):
    try:
        activeApps = NSWorkspace.sharedWorkspace().runningApplications()
        for app in activeApps:
            if app.isActive() and app.localizedName() != "Google Chrome":
                return
        if event.type() == NSKeyDown and keycode.tostring(
                event.keyCode()) in string.printable:
            if event.keyCode() == 48:
                sp.capture(region=region)
                currPiece = getPiece(
                    sp.pixel(topLeftCenterx + 4 * squareWidth, topLeftCentery))
                holdPiece = None
                isFirstHold = True
                while (1):
                    grid = [[False for i in range(19)] for j in range(10)]
                    colHeights = [0 for i in range(10)]
                    for i in range(10):
                        centerx = topLeftCenterx + i * squareWidth
                        for j in range(1, 20):
                            centery = topLeftCentery + j * squareWidth
                            if not (isEmpty(sp.pixel(centerx, centery))):
                                grid[i][19 - j] = True
                                if colHeights[i] == 0:
                                    colHeights[i] = 20 - j
                    colSums = [sum(grid[i]) for i in range(10)]
                    nextPiece = getPiece(sp.pixel(nextBoxx, nextBoxy))
                    if isFirstHold:
                        holdPiece = nextPiece
                    currMovementInfo = calculateBestPlacement(
                        currPiece, nextPiece, colHeights)
                    holdMovementInfo = None
                    if isFirstHold:
                        holdMovementInfo = calculateBestPlacement(
                            holdPiece, None, colHeights)
                    else:
                        holdMovementInfo = calculateBestPlacement(
                            holdPiece, nextPiece, colHeights)
                    if currMovementInfo[2] <= holdMovementInfo[2]:
                        executeMovement(currPiece, currMovementInfo[0],
                                        currMovementInfo[1])
                    else:
                        pressNTimes("c", 1)
                        if isFirstHold:
                            sleep(0.07)
                            sp.capture(region=region)
                            nextPiece = getPiece(sp.pixel(nextBoxx, nextBoxy))
                            isFirstHold = False
                        executeMovement(holdPiece, holdMovementInfo[0],
                                        holdMovementInfo[1])
                        holdPiece = currPiece
                    sp.capture(region=region)
                    currPiece = nextPiece

    except (KeyboardInterrupt) as e:
        print 'Ending', e
        AppHelper.stopEventLoop()
예제 #3
0
파일: TBotHole.py 프로젝트: mmwang/TBot
def handler(event):
    try:
	activeApps = NSWorkspace.sharedWorkspace().runningApplications()
	for app in activeApps:
		if app.isActive() and app.localizedName() != "Google Chrome":
			return
	if event.type() == NSKeyDown and keycode.tostring(event.keyCode()) in string.printable:
		if event.keyCode() == 48:
			global delay
			sp.capture(region=region)
			currPiece = getPiece(sp.pixel(topLeftCenterx + 4 * squareWidth, topLeftCentery))
			holdPiece = None
			isFirstHold = True
			while(1):
				colHeights = [0 for i in range(10)]
				grid = [[0 for i in range(23)] for i in range(10)]
				for i in range(10):
					centerx = topLeftCenterx + i * squareWidth
					for j in range(1, 20):
						centery = topLeftCentery + j * squareWidth
						if(not(isEmpty(sp.pixel(centerx, centery)))):
							grid[i][19 - j] = True
							if colHeights[i] == 0:
								colHeights[i] = 20 - j
				nextPiece = getPiece(sp.pixel(nextBoxx, nextBoxy))
				if isFirstHold:
					holdPiece = nextPiece
				currMovementInfo = calculateBestPlacement(currPiece, nextPiece, colHeights)
				holdMovementInfo = None
				if isFirstHold:
					holdMovementInfo = calculateBestPlacement(holdPiece, None, colHeights)
				else:
					holdMovementInfo = calculateBestPlacement(holdPiece, nextPiece, colHeights)
				
				if currMovementInfo[2] <= holdMovementInfo[2]:
					placePiece(currPiece, currMovementInfo[0], currMovementInfo[1], colHeights, grid)
					executeMovement(currPiece, currMovementInfo[0], currMovementInfo[1])
				else:
					pressNTimes("c", 1)
					if isFirstHold:
						sleep(0.07)
						sp.capture(region=region)
						nextPiece = getPiece(sp.pixel(nextBoxx, nextBoxy))
						isFirstHold = False
					placePiece(holdPiece, holdMovementInfo[0], holdMovementInfo[1], colHeights, grid)
					executeMovement(holdPiece, holdMovementInfo[0], holdMovementInfo[1])
					holdPiece = currPiece
				if lineCleared(grid):
					sleep(0.3)
				else:
					sleep(0.07)
				sp.capture(region=region)
				currPiece = nextPiece

    except ( KeyboardInterrupt ) as e:
        print 'Ending', e
        AppHelper.stopEventLoop()
    def handler(self, event):

        try:
            activeApps = self.workspace.runningApplications()
            for app in activeApps:
                if app.isActive():
                    if app.localizedName() != self.currentApp:
                        self.currentApp = app.localizedName()
                        options = kCGWindowListOptionOnScreenOnly
                        windowList = CGWindowListCopyWindowInfo(
                            options, kCGNullWindowID)

                        for window in windowList:
                            if window['kCGWindowOwnerName'] == self.currentApp:
                                geom = window['kCGWindowBounds']
                                self.screen_hook(
                                    event=event,
                                    name=window['kCGWindowName'],
                                    owner=window['kCGWindowOwnerName'],
                                    x=geom['X'],
                                    y=geom['Y'],
                                    w=geom['Width'],
                                    h=geom['Height'])
                                break
                    break

            loc = NSEvent.mouseLocation()

            # mouse clicky buttons
            if event.type() in (NSLeftMouseDown, NSRightMouseDown,
                                NSLeftMouseUp, NSRightMouseUp):
                self.mouse_button_hook(event=event, x=loc.x, y=loc.y)

            # mouse scrolly buttons
            elif event.type() == NSScrollWheel:
                if event.deltaY() > 0 and event.deltaY() < 0:
                    self.mouse_button_hook(event=event, x=loc.x, y=loc.y)
                if event.deltaX() > 0 and event.deltaX() < 0:
                    self.mouse_button_hook(event=event, x=loc.x, y=loc.y)

            # keys down
            elif event.type() in (NSKeyDown, NSKeyUp):

                flags = event.modifierFlags()
                modifiers = []  # OS X api doesn't care it if is left or right
                if (flags & NSControlKeyMask):
                    modifiers.append('CONTROL')
                if (flags & NSAlternateKeyMask):
                    modifiers.append('ALTERNATE')
                if (flags & NSCommandKeyMask):
                    modifiers.append('COMMAND')

                self.key_hook(event=event,
                              key=event.keyCode(),
                              char=keycode.tostring(event.keyCode()),
                              mods=modifiers,
                              is_repeat=event.isARepeat())

            # Mouse moved
            elif event.type() == NSMouseMoved:
                self.mouse_move_hook(event=event, x=loc.x, y=loc.y)
            else:
                pass

        except (KeyboardInterrupt) as e:
            print('handler', e)
            AppHelper.stopEventLoop()
예제 #5
0
#!/usr/bin/env python

import keycode
import sys

for arg in sys.argv[1:]:
    if arg and arg[0] == '-':
        print arg[1:], keycode.tostring(int(arg[1:]))
    else:
        for ch in arg:
            print ch, keycode.tokeycode(ch)
예제 #6
0
def handler(event):
    # if event.type() == NSKeyDown and keycode.tostring(event.keyCode()) in string.printable:
    if event.type() == NSKeyDown:
        print keycode.tostring(event.keyCode())
예제 #7
0
def handler(event):
    # if event.type() == NSKeyDown and keycode.tostring(event.keyCode()) in string.printable:
    if event.type() == NSKeyDown:
        print keycode.tostring(event.keyCode())