Print the current finger position on the touchpad¶
Shows how a device interaction can be implemented, that uses the touchpad of a VR controller. Further information on how a custom interaction can be implemented is shown in the “Implementation of a custom device interaction” example.
This device interaction creates two device actions, which are triggered when the touchpad is touched, or when the touch ends. When the touch starts, the moved signal of the controller is connected to a method that prints the position of the finger on the touchpad. The information about the position is part of the button state and can be queried from the device.
25padPosition = device.getButtonState("Touchpad").getPosition()
The moved signal is disconnectd, when the touchpad is not touched anymore, which ends the printing of the position.
1# © 2024 Autodesk, Inc. All rights reserved.
2
3class pad:
4 def __init__(self):
5 self.readPadPosition = False
6 self.currentController = ""
7
8 # Create an intreraction
9 self.padInteraction = vrDeviceService.createInteraction("PadInteraction")
10 # Set an interaction group that is supported, as other groups already use the touchpad
11 self.padInteraction.setSupportedInteractionGroups(["PadMode"])
12
13 # Get the actions that will be used
14 self.enableAction = self.padInteraction.createControllerAction("any-touchpad-touched")
15 self.disableAction = self.padInteraction.createControllerAction("any-touchpad-untouched")
16
17 # Connect to the methods that will de-/activate the reading of the touchpad position
18 # and the actual printing
19 self.enableAction.signal().triggered.connect(self.enablePad)
20 self.disableAction.signal().triggered.connect(self.disablePad)
21
22 # Set the interaction group active
23 vrDeviceService.setActiveInteractionGroup("PadMode")
24
25 def printPosition(self, device):
26 # Get the position of the finger on the touchpad and print it
27 padPosition = device.getButtonState("Touchpad").getPosition()
28 print(("Touchpad position: " + str(padPosition.x()) + " " + str(padPosition.y())))
29
30 def enablePad(self, action, device):
31 # If position reading is already active, do not do anything
32 if self.readPadPosition:
33 return
34
35 # Store which controller is currently used
36 self.currentController = device.getName()
37 # Activate the reading
38 self.readPadPosition = True
39 device.signal().moved.connect(self.printPosition)
40
41 def disablePad(self, action, device):
42 # If position reading is not active, do not do anything
43 if not self.readPadPosition:
44 return
45
46 # Check if this is the controller that activated the position printing
47 if self.currentController != device.getName():
48 return
49
50 # Deactivate the position reading and printing
51 self.readPadPosition = False
52 device.signal().moved.disconnect(self.printPosition)
53
54thePad = pad()