[UWP][XAML] Get GIF Image information from on Screen KeyBoard of Windows 11

Md. Niaz Mahmud 171 Reputation points
2023-01-04T13:22:19.447+00:00

Hello,

Is it possible to get gif image information, for example image filepath, from on screen keyboard gif selection? I can have keydown event in UWP, but all the information I can get is Ctrl+v was clicked from callback argument. But can not get any image file path. My app needs to show image on canvas though gif is clicked through on screen keyboard while I am on a textbox. But I can not get any image information, and textbox can not show gif image. Can anyone please help?

276050-a1672838644940.jpg

Developer technologies | Universal Windows Platform (UWP)
{count} votes

Accepted answer
  1. Anonymous
    2023-01-06T03:07:04.747+00:00

    Hello,

    Welcome to Microsoft Q&A!

    I understand your question now. You are trying to find a way to get the gif image that selected from the touch keyboard.

    When you select the GIF image from the touch keyboard, it is actually be copied to the Clipboard. Then the system will automatically trigger the paste action. That's why you will get the Control +V key in the TextBox keydown event.

    So what you need to do is just get the image from the Clipboard when you receive Control +V key in the TextBox keydown event. Also, you can't display the gif in the Textbox, you will need to use Image control to display the GIF file.

    I've made a simple demo to show how to get GIF image from the Clipboard.

    Xaml:

     <Grid>  
            <Grid.RowDefinitions>  
                <RowDefinition Height="*"/>  
                <RowDefinition Height="*"/>  
            </Grid.RowDefinitions>  
            <TextBox x:Name="inputBox" Width="300" Height="200" KeyDown="inputBox_KeyDown"/>  
            <Image x:Name="displayimage" Grid.Row="1" />  
        </Grid>  
    

    Code-behind:

      public bool isControlPressed { get; set; }  
            public MainPage()  
            {  
                this.InitializeComponent();  
                isControlPressed = false;  
            }  
      
            private async void inputBox_KeyDown(object sender, KeyRoutedEventArgs e)  
            {  
                // use a flag to check if control is pressed  
                if (e.Key == Windows.System.VirtualKey.Control)   
                {  
                    isControlPressed = true;  
                }  
                // if control and V are pressed, try to get the bitmapimage data from clipboard  
                if( e.Key == Windows.System.VirtualKey.V && isControlPressed)  
                {  
                    //get clipboard  
                    var dataPackageView = Clipboard.GetContent();  
                    if (dataPackageView.Contains(StandardDataFormats.Bitmap))  
                    {  
                        IRandomAccessStreamReference imageReceived = null;  
                        try  
                        {  
                            imageReceived = await dataPackageView.GetBitmapAsync();  
                        }  
                        catch (Exception ex)  
                        {  
                            Debug.WriteLine("failed to get bitmap: "+ ex.Message);  
                        }  
      
                        if (imageReceived != null)  
                        {  
                            using (var imageStream = await imageReceived.OpenReadAsync())  
                            {  
                                var bitmapImage = new BitmapImage();  
                                bitmapImage.SetSource(imageStream);  
                                displayimage.Source = bitmapImage;  
                            }  
                            // reset the falg  
                            isControlPressed = false;  
                        }  
                    }  
                    else  
                    {  
                        // no bitmap data, maybe other types  
                    }  
                }  
      
      
                Debug.WriteLine("inputBox_KeyDown");  
            }  
    

    Thank you.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.