Click here to Skip to main content
15,441,434 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My code rotates a cube when Key.Q is held down. The animation looks smooth. If I press and hold Key.Q and then I press and hold Shift, Alt, Ctrl or CapsLock, the animation visibly stutters (holding Tab, Backspace or Enter does not). If I'm holding Key.Q and a modifier key and I release the modifier key the animation is smooth again. This is undesirable behavior and I want the animation to not be affected if these other keys are held.

Is this a background process being captured and if so is there an override for this behavior?

<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


What I have tried:

I've tried other keys besides Q.

I've made several attempts to capture if modifier keys are held, i.e. "If e.Key = Key.LeftShift Or e.Key = Key.RightShift Then..." to capture the keydown and handle and exit the event but this hasn't made any noticeable difference.
Posted
Updated 7-Aug-22 17:38pm
v2
Comments
Graeme_Grant 6-Aug-22 23:29pm    
1ms on the timer is too often. For starters, I would dial that back.
Peter_in_2780 7-Aug-22 2:04am     CRLF
It's probably happening because modifier keys are processed more thoroughly by the system, looking for global shortcuts etc. But no, I have no idea how to circumvent that.
Gerry Schmitz 7-Aug-22 12:46pm    
A 1 ms timer tick is like "1000 frames per second". You should start by dialing it back to about 50 FPS. VR runs at 90 - 120 FPS.
StanGertrudeMeier 7-Aug-22 12:58pm    
I've tried several different numbers for the timer but I'm having the same issue.

1 solution

Setting the DispatcherTimer priority to Render solved the issue. The default priority is Background so I assume holding the modifier keys was calling a non-idle operation that was being given priority. Setting it to Render fixed the animation stuttering.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900