Mouse#
Reading the Mouse Cursor Location#
Two functions are provided for reading the current location of the mouse cursor. get_mouse_coords_pixel
returns the position within the app’s main window in terms of pixels. get_mouse_coords_normalized
also returns the cursor’s position but with a normalized value from 0 to 1. If the cursor is outside of the main window then the values aren’t updated and return the last valid position
import omni
import carb
self.input = carb.input.acquire_input_interface()
self.mouse = omni.appwindow.get_default_app_window().get_mouse()
(x,y) = self.input.get_mouse_coords_pixel(self.mouse)
print(f"get_mouse_coords_pixel : {x,y}")
(x, y) = self.input.get_mouse_coords_normalized(self.mouse)
print(f"get_mouse_coords_normalized : {x, y}")
Reading the Mouse State#
Mouse input currently doesn’t support event callbacks, therefore you must poll for the values. The below code demonstrates printing the values of each action state. These states will only be non-zero when you are performing an action such as pressing a button, moving the mouse or using the scroll wheel. If this is put in an update loop it will only print when there is an action taking place.
import omni
import carb
self.input = carb.input.acquire_input_interface()
self.mouse = omni.appwindow.get_default_app_window().get_mouse()
val = self.input.get_mouse_value(self.mouse, carb.input.MouseInput.LEFT_BUTTON)
if val: print(f"LEFT_BUTTON : {val}")
val = self.input.get_mouse_value(self.mouse, carb.input.MouseInput.MIDDLE_BUTTON)
if val: print(f"MIDDLE_BUTTON : {val}")
val = self.input.get_mouse_value(self.mouse, carb.input.MouseInput.RIGHT_BUTTON)
if val: print(f"RIGHT_BUTTON : {val}")
val = self.input.get_mouse_value(self.mouse, carb.input.MouseInput.FORWARD_BUTTON)
if val: print(f"FORWARD_BUTTON : {val}")
val = self.input.get_mouse_value(self.mouse, carb.input.MouseInput.SCROLL_RIGHT)
if val: print(f"SCROLL_RIGHT : {val}")
val = self.input.get_mouse_value(self.mouse, carb.input.MouseInput.SCROLL_LEFT)
if val: print(f"SCROLL_LEFT : {val}")
val = self.input.get_mouse_value(self.mouse, carb.input.MouseInput.SCROLL_UP)
if val: print(f"SCROLL_UP : {val}")
val = self.input.get_mouse_value(self.mouse, carb.input.MouseInput.SCROLL_DOWN)
if val: print(f"SCROLL_DOWN : {val}")
val = self.input.get_mouse_value(self.mouse, carb.input.MouseInput.MOVE_RIGHT)
if val: print(f"MOVE_RIGHT : {val}")
val = self.input.get_mouse_value(self.mouse, carb.input.MouseInput.MOVE_LEFT)
if val: print(f"MOVE_LEFT : {val}")
val = self.input.get_mouse_value(self.mouse, carb.input.MouseInput.MOVE_UP)
if val: print(f"MOVE_UP : {val}")
val = self.input.get_mouse_value(self.mouse, carb.input.MouseInput.MOVE_DOWN)
if val: print(f"MOVE_DOWN : {val}")
val = self.input.get_mouse_value(self.mouse, carb.input.MouseInput.COUNT)
if val: print(f"COUNT : {val}")