tags:

views:

70

answers:

5
+2  Q: 

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?

A: 

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

H4mm3rHead
+2  A: 

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.

Oliver
A: 

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;
Veer
thanks for your code, But I a looking for any events from wpf team such as keydown and keyUp event like, mean is there any such event for control. which can tell me any key is pressed ?
Asim Sajjad
A: 

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

Jobi Joy
I am creating of my own, I just want when textbox is in focus then by pressing the up and down arrow key then it will do increament and decrement. but the problem is keyup and keydown, which fires once for each key press.
Asim Sajjad
A: 

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
marco.ragogna