How to enable wpf window to listen to keyboard when window is not focused.

Sudhakar Pandey 1 Reputation point
2021-04-06T09:49:51.483+00:00

I have a wpf application with a window, say first window has a list view which shows some items and when any list view item is selected and user press left navigation keyboard button , application needs to launch new window and list view should selects next item.

To realize above requirement list view's previewkeydown event handler is being used to launch new window and change selection to next item.But the problem is once new window is open, new window will get focus and if user press left navigation keyboard button, list view's previewkeydown event handler does not get called. so to make left navigation key press work, once newly window is loaded, focus is being set on first window by using "window.Focus()" api. but this solution does not work always.

Is there a way to listen to keyboard key pressed event when window is not focused?

Developer technologies | Windows Presentation Foundation
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. DaisyTian-1203 11,646 Reputation points
    2021-04-07T03:11:20.737+00:00

    I made a sample to implement what you want with a ListView based on my understanding, if I misundertand your question, please point out.
    The Xaml code is:

    <Grid>  
                <ListView x:Name="listView1" Width="350" Height="400" ItemsSource="{Binding}" PreviewKeyDown="listView1_PreviewKeyDown">  
                    <ListView.View>  
                        <GridView>  
                            <GridViewColumn Header="Num" Width="80" DisplayMemberBinding="{Binding Num}" />  
                            <GridViewColumn Header="Name" Width="80" DisplayMemberBinding="{Binding Name}"/>  
                            <GridViewColumn Header="Type" Width="80"  DisplayMemberBinding="{Binding Tyep}" />  
                            <GridViewColumn Header="Author" Width="80"  DisplayMemberBinding="{Binding Author}"/>  
                        </GridView>  
                    </ListView.View>  
                </ListView>  
            </Grid>  
    

    The cs code is:

    public partial class MainWindow : Window  
        {  
            public MainWindow()  
            {  
                InitializeComponent();  
                initList();  
            }  
            public void initList()  
            {  
                List<Book> listBook = new List<Book>();  
                for (int i = 0; i < 15; i++)  
                {  
                    listBook.Add(new Book() { Num = "00" + i.ToString(), Name = "testBook" + i, Type = "Math", Author = "qiaobus" });  
                }  
                listView1.ItemsSource = listBook;  
            }  
      
            private void listView1_PreviewKeyDown(object sender, KeyEventArgs e)  
        {  
            if (e.Key == Key.Left)  
            {  
                DetailedWin win = new DetailedWin();  
                win.Show();  
    
                int selectedIndex = 0;  
                if (listView1.SelectedIndex > 0)  
                {  
                    selectedIndex = listView1.SelectedIndex;  
                }  
                if (selectedIndex <= listView1.Items.Count)  
                {  
                    listView1.SelectedIndex = selectedIndex + 1;  
                    ListViewItem row = (ListViewItem)listView1.ItemContainerGenerator.ContainerFromIndex(selectedIndex + 1);  
                    row.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));  
                }  
            }  
            
        }  
        }  
        public class Book  
        {  
            public string Num { get; set; }  
            public string Name { get; set; }  
            public string Type { get; set; }  
            public string Author { get; set; }  
        }  
    

    The Result picture is:
    85085-2.gif


    If the response is helpful, please click "Accept Answer" and upvote it.
    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.


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.