Browse Source

keyboard and mouse inputter now work

Eric Petersen 2 years ago
parent
commit
3df6eb48ef

+ 6 - 0
alphagriffin/hammerwatch_ai/__main__.py

@@ -5,6 +5,7 @@ plays gta5 tutorials. and other stuff.
 """
 # imports
 import screengrab
+import input_control
 
 __author__ = "Eric Petersen @Ruckusist"
 __copyright__ = "Copyright 2017, The Alpha Griffin Project"
@@ -17,9 +18,14 @@ __status__ = "Prototype"
 class App(object):
     def __init__(self):
         self.take = screengrab
+        self.inputter = input_control.Inputter()
 
     def main(self):
         img = self.take.grab_screen()
+        self.inputter.place_mouse(1600, 100)
+        self.inputter.key_press('w')
+        self.inputter.key_press('a')
+        self.inputter.key_press('s')
         print("Good test!")
         return True
 

+ 124 - 0
alphagriffin/hammerwatch_ai/input_control.py

@@ -0,0 +1,124 @@
+#!/usr/bin/env python
+"""Input Controller for keyboard and mouse.
+"""
+__author__ = "Eric Petersen @Ruckusist"
+__copyright__ = "Copyright 2017, The Alpha Griffin Project"
+__credits__ = ["Eric Petersen", "Shawn Wilson", "@alphagriffin"]
+__maintainer__ = "Eric Petersen"
+__email__ = "ruckusist@alphagriffin.com"
+__status__ = "Prototype"
+"""
+from pynput.mouse import Button, Controller
+import time
+
+mouse = Controller()
+
+# Read pointer position
+print('The current pointer position is {0}'.format(
+    mouse.position))
+
+# Set pointer position
+mouse.position = (1600, 100)
+print('Now we have moved it to {0}'.format(
+    mouse.position))
+
+# Move pointer relative to current position
+mouse.move(5, -5)
+
+# Press and release
+mouse.press(Button.left)
+mouse.release(Button.left)
+
+# Double click; this is different from pressing and releasing
+# twice on Mac OSX
+mouse.click(Button.left, 2)
+
+# Scroll two steps down
+mouse.scroll(0, 2)
+from pynput.keyboard import Key, Controller
+keyboard = Controller()
+
+# Press and release space
+#keyboard.press(Key.space)
+#keyboard.release(Key.space)
+
+# Type a lower case A; this will work even if no key on the
+# physical keyboard is labelled 'A'
+keyboard.press('w')
+keyboard.release('w')
+keyboard.press('a')
+time.sleep(.5)
+keyboard.release('a')
+keyboard.press('a')
+keyboard.release('a')
+# Type two upper case As
+#keyboard.press('A')
+#keyboard.release('A')
+#with keyboard.pressed(Key.shift):
+#    keyboard.press('a')
+#    keyboard.release('a')
+
+# Type 'Hello World' using the shortcut type method
+#img = '/tmp/screenshot.png'
+#keyboard.type('Hello World')
+print('\nthis is a test.')
+"""
+import pynput.keyboard as Keyboard
+import pynput.mouse as Mouse
+import time
+
+class Inputter(object):
+    def __init__(self):
+
+        self.keyboard = Keyboard.Controller()
+        self.keyboard_key = Keyboard.Key
+        self.mouse = Mouse.Controller()
+        self.mouse_button = Mouse.Button
+
+    def get_mouse(self):
+        # get mouse position
+        mouse_position = self.mouse.position
+        return mouse_position
+
+    def get_mouse_button(self):
+        position_of_click = 0
+        return position_of_click
+
+    def place_mouse(self, x, y):
+        # place mouse
+        self.mouse.position = (x, y)
+        return True
+
+    def scroll_mouse(y):
+        self.mouse.scroll(0, y)
+        return True
+
+    def get_keyboard(self):
+        # get keyboard combo
+        keyboard_combo = 0
+        return keyboard_combo
+
+    def key_press(self, keys):
+        # press key combo
+        self.keyboard.press(keys)
+        time.sleep(.1)
+        self.keyboard.release(keys)
+        return True
+
+def main():
+    # TODO: this is still missing a command line arg parser!
+    app = Inputter()
+    try:
+        print(app.get_mouse())
+        app.place_mouse(1600, 100)
+
+    except Exception as e:
+        print("The bugger at Fault:\n\t{}".format(e))
+        pass
+
+if __name__ == '__main__':
+    try:
+        main()
+        print("(c) 2017 || alphagriffin.com")
+    except Exception as e:
+        print("{}\nand thats okay too.".format(e))

+ 14 - 0
alphagriffin/hammerwatch_ai/prep_img.py

@@ -0,0 +1,14 @@
+#!/usr/bin/env python
+"""Cv2 implementation on game graphics...
+maybe tesseract too.
+"""
+__author__ = "Eric Petersen @Ruckusist"
+__copyright__ = "Copyright 2017, The Alpha Griffin Project"
+__credits__ = ["Eric Petersen", "Shawn Wilson", "@alphagriffin"]
+__maintainer__ = "Eric Petersen"
+__email__ = "ruckusist@alphagriffin.com"
+__status__ = "Prototype"
+
+import cv2
+
+print('\nthis is a test.')