This is a vb.net WPF application.
Holding modifier keys [Shift, Alt, Ctrl] slows this application.
My code rotates a cube when Key.Q is held down. Pressing Shift Q, Alt Q or Ctrl Q visibly slows down the rotation.
Why is this happening and how to I disable the background key press function that's being called?
Edit - I've tried handling key presses for the modifier keys, i.e. adding variations of "'If e.Key = Key.LeftShift Or e.Key = Key.RightShift Then..." to the key press events but these have not helped.
<Viewport3D Name="ViewportMain">
<Viewport3D.Camera>
<PerspectiveCamera x:Name="camMain" Position="6 5 4" LookDirection="-6 -5 -4">
</PerspectiveCamera>
</Viewport3D.Camera>
<ModelVisual3D>
<ModelVisual3D.Content>
<AmbientLight Color="#ffffff" />
</ModelVisual3D.Content>
</ModelVisual3D>
<ModelVisual3D x:Name="MyModel">
<ModelVisual3D.Content>
<GeometryModel3D>
<GeometryModel3D.Geometry>
<MeshGeometry3D x:Name="meshMain"
Positions="0 0 0 1 0 0 0 1 0 1 1 0 0 0 1 1 0 1 0 1 1 1 1 1"
TriangleIndices="2 3 1 2 1 0 7 1 3 7 5 1 6 5 7 6 4 5 6 2 0 2 0 4 2 7 3 2 6 7 0 1 5 0 5 4">
</MeshGeometry3D>
</GeometryModel3D.Geometry>
<GeometryModel3D.Material>
<DiffuseMaterial x:Name="matDiffuseMain">
<DiffuseMaterial.Brush>
<SolidColorBrush Color="Red"/>
</DiffuseMaterial.Brush>
</DiffuseMaterial>
</GeometryModel3D.Material>
</GeometryModel3D>
</ModelVisual3D.Content>
<ModelVisual3D.Transform>
<RotateTransform3D>
<RotateTransform3D.Rotation>
<AxisAngleRotation3D x:Name="rotate" Axis="0 2 0"/>
</RotateTransform3D.Rotation>
</RotateTransform3D>
</ModelVisual3D.Transform>
</ModelVisual3D>
</Viewport3D>
Public WithEvents Timer1 As New System.Windows.Threading.DispatcherTimer()
Public ax3d As Media3D.AxisAngleRotation3D
Public myRotateTransform As Media3D.RotateTransform3D
Public isrotating As Boolean = False
Private Sub Window_Loaded(sender As Object, e As RoutedEventArgs)
ax3d = New Media3D.AxisAngleRotation3D(New Media3D.Vector3D(0, 2, 0), 1)
myRotateTransform = New Media3D.RotateTransform3D(ax3d)
MyModel.Transform = myRotateTransform
Timer1.IsEnabled = True
Timer1.Interval = TimeSpan.FromMilliseconds(1)
Timer1.Start()
End Sub
Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
If isrotating Then
ax3d.Angle = 4
End If
End Sub
Private Sub Window_KeyDown(sender As Object, e As KeyEventArgs)
If e.IsRepeat Then
e.Handled = True
Return
End If
If e.Key = Key.Q Then
isrotating = True
e.Handled = True
End If
e.Handled = True
End Sub
Private Sub Window_KeyUp(sender As Object, e As KeyEventArgs)
If e.Key = Key.Q Then
isrotating = False
e.Handled = True
End If
End Sub
CodePudding user response:
Setting the priority for the DispatcherTimer to Render priority fixed the issue. The default priority is Background.
"Operations are processed after all other non-idle operations are completed."
Holding the modifier keys Shift, Alt, and Ctrl, as well as CapsLock, would interfere with the DispatcherTimer and cause the timer ticks to be delayed.
Setting the priority to Render allows operations to be processed at the same priority as rendering. As a result the animation no longer stutters.