Asim Sajjad
Asim Sajjad

Reputation: 2534

KeyPressed Event

My Problem is that I want to check if the Arrow up or down key is press then I want to increment or decrement value in the textbox control. I have registered keyup event but I have to release the arrow up key in order to change the value, What I want is if User pressed up arrow key then it will increment the value until user release up arrow key and same for the down arrow key. Any idea?

Upvotes: 2

Views: 629

Answers (5)

Drake
Drake

Reputation: 8392

I create an NumericTextBox with Up and Down button using this technique.

In XAML you have something like:

<Button x:Name="bntUp"                                 
        Command="{x:Static local:IntegerTextBox.UpCommand}"
        Width="10" Grid.Row="0" Cursor="Hand"
        HorizontalAlignment="Right">
    <Button.Template>
        <ControlTemplate TargetType="Button">
            <Border Margin="1" Background="{StaticResource UpArrowBrush}" />
        </ControlTemplate>
    </Button.Template>
</Button>
<Button x:Name="bntDown"                                 
        Command="{x:Static local:IntegerTextBox.DownCommand}"
        Width="10" Grid.Row="1" Cursor="Hand"
        HorizontalAlignment="Right">
    <Button.Template>
        <ControlTemplate TargetType="Button">
            <Border Margin="1" Background="{StaticResource DownArrowBrush}" />
        </ControlTemplate>
    </Button.Template>
</Button>

In code behind (for up/increase part):

 Private Shared _UpCommand As RoutedCommand
    Private Shared _DownCommand As RoutedCommand

    Public Shared ReadOnly Property UpCommand() As RoutedCommand
        Get
            Return _UpCommand
        End Get
    End Property

    Private Shared Sub InitializeCommands()

        _UpCommand = New RoutedCommand("UpCommand", GetType(IntegerTextBox))
        CommandManager.RegisterClassCommandBinding(GetType(IntegerTextBox), New CommandBinding(_UpCommand, AddressOf OnUpCommand))
        CommandManager.RegisterClassInputBinding(GetType(IntegerTextBox), New InputBinding(_UpCommand, New KeyGesture(Key.Up)))

    End Sub

    Private Shared Sub OnUpCommand(ByVal sender As Object, ByVal e As ExecutedRoutedEventArgs)

        Dim itb As IntegerTextBox = TryCast(sender, IntegerTextBox)
        If itb Is Nothing Then Return
        itb.OnUp()

    End Sub

Protected Overridable Sub OnUp()

        Dim caretIndex As Integer = Me.CaretIndex

        Me.Value += 1

        Me.GetBindingExpression(IntegerTextBox.TextProperty).UpdateSource()

        If caretIndex <= Me.Text.Length Then Me.CaretIndex = caretIndex Else Me.CaretIndex = Me.Text.Length

    End Sub

Upvotes: 0

Jobi Joy
Jobi Joy

Reputation: 50038

Are you sure you want just a RepeatButton of WPF? which has this functionality in it

Upvotes: 0

Amsakanna
Amsakanna

Reputation: 12934

bool isIncrement = true;

keyDown Execute Method:

StopWatch s = StopWatch.StartNew();
int timeInterval = 500; // for 0.5 second
i=1;
while(1)
{
    if((int)s.ElapsedMilliseconds / timeInterval < i++ )
        continue;
    if(isIncrement)
    {
        //increment value
    }
    else
    {
        s.Stop();
        return;
    }
}

keyUp Execute Method:

isIncrement = false;

Upvotes: 0

Oliver
Oliver

Reputation: 45101

Instead of using the KeyUp event you should take the KeyDown event. This will also be sent multiple times if you just hold the key.

Upvotes: 4

Brian Hvarregaard
Brian Hvarregaard

Reputation: 4209

HAve you tried registrering the KeyDown event for setting the flag, and then the KeyUp event for unsetting the flag?

Upvotes: 0

Related Questions