0
votes

I want to use ctrl-V to paste images into image control in WPF and windows 10 UWP application.

Image control has keydown, keyup events. But it does not seem to ever fire. Selecting image control (so it has focus), and pressing any key does not fire any of the keyboard events.

Anyone know what's going on?

Thanks.

1

1 Answers

2
votes

Handle WPF Image control's KeyUP and KeyDown won't work, we have to put the KeyUp & KeyDown handler at the window level.

<Window x:Class="WPFImageControl.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525" KeyDown="Window_KeyDown" KeyUp="Window_KeyUp">
    <Grid>
        <Image x:Name="image1" Source="build.png" />
    </Grid>
</Window>

Code Behind:

private void Window_KeyDown(object sender, KeyEventArgs e)
{
    Debug.WriteLine("Window KeyDown: " + e.Key + " Time:" +DateTime.Now.ToString());
}

private void Window_KeyUp(object sender, KeyEventArgs e)
{
    Debug.WriteLine("Window KeyUp: " + e.Key + " Time:" + DateTime.Now.ToString());
}