question

SudhakarPandey-4578 avatar image
0 Votes"
SudhakarPandey-4578 asked DaisyTian-1203 commented

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


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?

windows-wpf
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

DaisyTian-1203 avatar image
0 Votes"
DaisyTian-1203 answered DaisyTian-1203 commented

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.



2.gif (453.2 KiB)
· 5
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thank you for response but solution given here does not work always.

0 Votes 0 ·

@SudhakarPandey-4578
This sample is working for me, is there anything I missed about your requirement? By the way, the answer for getting focused element/control in a WPF window may give you some help.


0 Votes 0 ·

@SudhakarPandey-4578
May I know if you have got any chance to check my reply? Does it work for you?I am looking forward to seeing your message.

0 Votes 0 ·

@DaisyTian-MSFT
I have tried with your solution but it does not work always. For simple project where in you have less amount of content in other window your solution works fine, but when solution is integrated with my project where opening window has more contents, focus remains with newly opened window.

0 Votes 0 ·
Show more comments