Advertisement
Guest User

Untitled

a guest
Mar 2nd, 2013
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.25 KB | None | 0 0
  1. ################################################################################
  2. # Copyright (C) 2012-2013 Leap Motion, Inc. All rights reserved. #
  3. # Leap Motion proprietary and confidential. Not for distribution. #
  4. # Use subject to the terms of the Leap Motion SDK Agreement available at #
  5. # https://developer.leapmotion.com/sdk_agreement, or another agreement #
  6. # between Leap Motion and you, your company or other organization. #
  7. ################################################################################
  8.  
  9. #IMPORTS
  10. import Leap, sys
  11. import ctypes
  12. import win32api, win32con
  13. from time import sleep
  14.  
  15. class LeapListener(Leap.Listener):
  16.  
  17. DEBUG = False
  18. fingers_count = 0
  19. prev_fingers_count = 0
  20. SLOW = 0.010;
  21.  
  22. #Screen resolution, it should match the current screen resolution for more precise movements
  23. SCREEN_X = 0
  24. SCREEN_Y = 0
  25.  
  26. cur_x = 0
  27. cur_y = 0
  28.  
  29. Lclicked = False
  30. Rclicked = False
  31. keystroke = False
  32. LHold = False
  33.  
  34. #In regards to the computer
  35. def on_init(self, controller):
  36. user32 = ctypes.windll.user32
  37. #gets screen resolution
  38. self.SCREEN_X = user32.GetSystemMetrics(0)
  39. self.SCREEN_Y = user32.GetSystemMetrics(1)
  40. print "Current screen resolution: {0} x {1}".format(self.SCREEN_X, self.SCREEN_Y)
  41.  
  42. print "Initialized"
  43.  
  44. #In regards to the leap connection
  45.  
  46. def on_connect(self, controller):
  47. print "Connected"
  48.  
  49. def on_disconnect(self, controller):
  50. print "Disconnected"
  51.  
  52. def on_exit(self, controller):
  53. print "Exited"
  54.  
  55.  
  56. #Every frame it will check to do this.
  57. def on_frame(self, controller):
  58.  
  59. # Get the most recent frame and report some basic information
  60. frame = controller.frame()
  61.  
  62. if not frame.fingers.empty:
  63. # Get Fingers
  64. fingers = frame.fingers
  65. self.fingers_count = fingers.__len__();
  66.  
  67. #If its on debug mode and the number of fingers showing is not the same as previous print, it willl do this
  68.  
  69. if LeapListener.DEBUG and self.fingers_count != self.prev_fingers_count:
  70. print "Currently %s fingers visible.\n" % self.fingers_count
  71. self.prev_fingers_count = self.fingers_count
  72.  
  73.  
  74. if not fingers.empty:
  75. # Calculate the hand's average finger tip position
  76. avg_pos = Leap.Vector()
  77. for finger in fingers:
  78. avg_pos += finger.tip_position
  79. avg_pos /= len(fingers)
  80.  
  81. moveMouse(self, int(avg_pos.x*5), int(self.SCREEN_X - avg_pos.y*1))
  82.  
  83. # Left Click
  84. if self.fingers_count == 1 and not self.Lclicked and avg_pos.z<=-70:
  85. clickMouse(self.cur_x, self.cur_y, 0)
  86. releaseMouse(self.cur_x, self.cur_y, 0)
  87. self.Lclicked = True
  88.  
  89. if self.DEBUG:
  90. print("LClicked")
  91. else:
  92. if self.fingers_count != 1 or avg_pos.z >0:
  93. self.Lclicked = False
  94. slow(self)
  95.  
  96.  
  97. # Left Click Hold
  98. if self.fingers_count == 2 and not self.LHold and avg_pos.z<=-70:
  99. clickMouse(self.cur_x, self.cur_y, 0)
  100. self.LHold = True
  101.  
  102. if self.DEBUG:
  103. print("LHold")
  104. else:
  105. if self.fingers_count != 2 or avg_pos.z >0:
  106. if self.LHold:
  107. releaseMouse(self.cur_x, self.cur_y, 0)
  108. self.LHold = False
  109. slow(self)
  110.  
  111. # Right Click
  112. if self.fingers_count == 3 and not self.Rclicked and avg_pos.z<=-70:
  113. clickMouse(self.cur_x, self.cur_y, 1)
  114. releaseMouse(self.cur_x, self.cur_y, 1)
  115. self.Rclicked = True
  116.  
  117. if self.DEBUG:
  118. print("RClicked")
  119. else:
  120. if self.fingers_count != 3 or avg_pos.z >0:
  121. self.Rclicked = False
  122. slow(self)
  123.  
  124.  
  125. def slow(self):
  126. sleep(self.SLOW)
  127.  
  128. def moveMouse(self, x, y):
  129. if self.cur_x != x or self.cur_y != y:
  130. self.cur_x = x
  131. self.cur_y = y
  132. win32api.SetCursorPos((int(x),int(y)))
  133.  
  134. # 0: Left
  135. # 1: Right
  136. # 2: Middle -not implemented yet-
  137. def clickMouse(cur_x, cur_y, value):
  138.  
  139. if value == 0:
  140. win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,cur_x, cur_y,0,0)
  141. else:
  142. if value == 1:
  143. win32api.mouse_event(win32con.MOUSEEVENTF_RIGHTDOWN,cur_x, cur_y,0,0)
  144. else:
  145. if value == 2:
  146. win32api.mouse_event(win32con.MOUSEEVENTF_MIDDLEDOWN,cur_x, cur_y,0,0)
  147.  
  148. # 0: Left
  149. # 1: Right
  150. # 2: Middle -not implemented yet-
  151. def releaseMouse(cur_x, cur_y, value):
  152.  
  153. if value == 0:
  154. win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,cur_x, cur_y,0,0)
  155. else:
  156. if value == 1:
  157. win32api.mouse_event(win32con.MOUSEEVENTF_RIGHTUP,cur_x, cur_y,0,0)
  158. else:
  159. if value == 2:
  160. win32api.mouse_event(win32con.MOUSEEVENTF_MIDDLEUP,cur_x, cur_y,0,0)
  161.  
  162.  
  163. def main():
  164. # Create a sample listener and controller
  165. listener = LeapListener()
  166. controller = Leap.Controller()
  167.  
  168. if raw_input('Do you want to enable Debug Mode? Y/N (Default: Disabled)').lower() == 'y':
  169. LeapListener.DEBUG = True
  170. print "Debug Mode Enabled."
  171. else:
  172. print "Default: Debug Mode Disabled."
  173.  
  174.  
  175.  
  176. # Have the sample listener receive events from the controller
  177. controller.add_listener(listener)
  178.  
  179. # Keep this process running until Enter is pressed
  180. print "Press Enter to quit..."
  181. sys.stdin.readline()
  182.  
  183. # Remove the sample listener when done
  184. controller.remove_listener(listener)
  185.  
  186.  
  187. if __name__ == "__main__":
  188. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement