TOrientationSensor is a component in the FireMonkey library in Delphi that allows you to detect and respond to changes in device orientation. It is similar to the TMotionSensor component, but it is specifically designed for handling orientation changes.
To use the TOrientationSensor component in Delphi, you will first need to add the FMX.Sensors unit to your uses clause. Then, you can drop a TOrientationSensor component onto your form and configure its properties, such as the interval at which it should update. Finally, you can handle the OnSensorChange event to respond to changes in device orientation.
Here is an example of using a TOrientationSensor component in Delphi:
1 2 3 4 5 6 7 8 9 10 11 12 |
uses FMX.Sensors; procedure TForm1.FormCreate(Sender: TObject); begin OrientationSensor1.Active := True; end; procedure TForm1.OrientationSensor1SensorChange(Sender: TObject; const OldState, NewState: TSensorState); begin Label1.Text := Format(‘Roll: %f, Pitch: %f, Yaw: %f’, [NewState.Orientation.Roll, NewState.Orientation.Pitch, NewState.Orientation.Yaw]); end; |
This code will activate the orientation sensor when the form is created, and then updates the label with the new orientation values in Roll, Pitch and Yaw each time the sensor is changed.
You can also use the Interval
property to specify the interval at which the sensor should update. And like the TMotionSensor, you can use the IsSensorSupported
method before using a Orientation sensor.
It’s worth noting that not all devices have all the types of sensors, so you should check the IsSensorSupported
method before using the Orientation sensor.
1 2 3 4 5 |
if OrientationSensor1.IsSensorSupported(TSensorType.Orientation) then begin OrientationSensor1.Active := True; end else ShowMessage(‘Orientation sensor is not supported.’); |
And you can use the OnActivate / OnDeactivate events to know when the sensor is available or not.
1 2 3 4 5 6 7 8 9 |
procedure TForm1.OrientationSensor1Activate(Sender: TObject); begin ShowMessage(‘Sensor activated’); end; procedure TForm1.OrientationSensor1Deactivate(Sender: TObject); begin ShowMessage(‘Sensor deactivated’); end; |
In summary, the TOrientationSensor component in Delphi allows you to easily detect and respond to changes in device orientation. You can use the properties and events of the component to configure and handle the sensor.
Leave a Reply