1

I'm programming a C# program which listens for Space pressed in the File Explorer. When space is pressed I create a WPF window which shows the selected File in a WPF Window.

The only problem is, that this file Preview is also called when someone is editing the filename and presses space for a whitespace in the filename.

Is there a way to detect if a user is renaming a file at the moment?


Additional Information:

I know that i could listen for F2, but there's also the way to start renaming a file by clicking two times with the left mouse button or by right clicking the file and selecting rename. So this would be no good solution.

Technical Information (if needed):

I use GetForegroundWindow to check if the Window in the foreground is a explorer window. Then i use Hooks to listen for the pressed Keys in the explorer process in foreground.

To get the selected Item i use SHDocVw and Shell32

                    Shell32.FolderItems selectedItems = ((Shell32.IShellFolderViewDual2) window.Document).SelectedItems();
user2974830
  • 428
  • 1
  • 4
  • 13
  • Just to clarify, you are trying to build a windows file preview function similar to the Finder on Mac OS X, whereby pressing space while a file is selected pops up the preview window? – Aaron D Jan 24 '15 at 19:02
  • Yes, that's exactly what i do/did. – user2974830 Jan 24 '15 at 19:04
  • Explorer already has a preview pane that's toggled with ALT+P. Are you trying to modify the behaviour of the shortcut? – Panagiotis Kanavos Jan 27 '15 at 10:57
  • Hi @PanagiotisKanavos no I'm not trying to modify the behaviour of the shortcut. I'd created my Preview which Pops up in front of the Explorer. I don't use the preview pane because of the lack of supported Formats and it's not really user friendly – user2974830 Jan 27 '15 at 11:02
  • 4
    A thought: how exactly are you hooking into key-presses? Global hooks? Can you share that code? With every keystroke notification, you should be getting the window handle (`HWND` / `IntPtr`) of the control where each keystroke takes place. You would have to check the window class of each such `HWND` and verify that it belongs to a list view (explorer view) and not a text box (file rename box). – Leandro Jan 27 '15 at 19:10
  • Hey @LeandroTaset, this is the code i used for hooking into key-presses: https://social.msdn.microsoft.com/Forums/vstudio/ar-SA/88ae8842-5301-4b15-830e-1d6282303508/how-to-listen-to-keyboard-inputs?forum=netfxbcl I tried another way to get the active hwnd of a window child. But I can't get the file rename box. do you have an idea how i could get this? – user2974830 Feb 01 '15 at 01:08
  • 1
    From the **MSDN** docs for [`SetWindowsHookEx`](https://msdn.microsoft.com/en-us/library/windows/desktop/ms644990), it seems that you would not be able to get the `HWND` of the focused child control that generated the event while using `WH_KEYBOARD_LL`. Perhaps `WH_CALLWNDPROC` or `WH_CALLWNDPROCRET` would serve you better in your scenario. However, check this article on **CodeProject**, it should provide some guidance on how to approach the problem using your current code: [Control in Focus in Other Processes](http://www.codeproject.com/Articles/34752/Control-in-Focus-in-Other-Processes). – Leandro Feb 01 '15 at 03:37
  • Thanks for the Article! I'll have a look at it. If you'd have tips for other approaches I'd be glad if you give them. I can change my code if it'll help me achieve the goal :) – user2974830 Feb 01 '15 at 19:59

2 Answers2

0

To detect file rename, check for FileSystemWatcher.Changed Event. Here is the example code taken from MSDN. MSDN FileSystemWatcher Example

I have slightly modified code. I have verified the code. It notifies as and when file is renamed.

using System;
using System.IO;
using System.Security.Permissions;

namespace FileWatcher
{
    class Program
    {
        static void Main(string[] args)
        {
            Run();
        }

        [PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
        public static void Run()
        {
            // Create a new FileSystemWatcher and set its properties.
            FileSystemWatcher watcher = new FileSystemWatcher();

            watcher.Path = @"c:\temp"; //Specify the directory name where file resides

            /* Watch for changes in LastAccess and LastWrite times, and
               the renaming of files or directories. */

            watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
               | NotifyFilters.FileName | NotifyFilters.DirectoryName;

            // Only watch text files.
            watcher.Filter = "*.txt";

            // Add event handlers.
            watcher.Changed += new FileSystemEventHandler(OnChanged);
            watcher.Created += new FileSystemEventHandler(OnChanged);
            watcher.Deleted += new FileSystemEventHandler(OnChanged);
            watcher.Renamed += new RenamedEventHandler(OnRenamed);

            // Begin watching.
            watcher.EnableRaisingEvents = true;

            // Wait for the user to quit the program.
            Console.WriteLine("Press \'q\' to quit the sample.");
            while (Console.Read() != 'q') ;
        }

        // Define the event handlers. 
        private static void OnChanged(object source, FileSystemEventArgs e)
        {
            // Specify what is done when a file is changed, created, or deleted.
            Console.WriteLine("File: " + e.FullPath + " " + e.ChangeType);
        }

        private static void OnRenamed(object source, RenamedEventArgs e)
        {
            // Specify what is done when a file is renamed.
            Console.WriteLine("File: {0} renamed to {1}", e.OldFullPath, e.FullPath);
        }
    }
}
Vinkal
  • 2,964
  • 1
  • 19
  • 19
  • The event `Renamed` occurs after? The way I read it was that the OP wants to trap it when file is being renamed, not after. – Jeremy Thompson Feb 03 '15 at 08:45
  • AFAIK, Renamed event occurs only after file is renamed. it does not occur when file is being renamed. please play with the code to confirm the same. – Vinkal Feb 03 '15 at 09:24
  • Hey @Coder, thanks for the try, but the Renamed event only occurs after the file is renamed, so it's not applicable for my problem. – user2974830 Feb 03 '15 at 13:12
0

Your file navigation enhancement should not risk interfering with the behaviour currently present in Windows Explorer.

Hence:

Ctrl+Space

Using a new command such as Ctrl+Space will not get triggered during a file-rename operation plus your application will work using the standard baked in Windows OS user-commands (with a little spice).

  • Ctrl+Space is an IntelliSense command for us dev's, so its a natural command for people to use for "more info"/"help me out".

I hope you have a very special File Preview. In the last 2 decades there have been a lot of cases where it has been a PITA not a pleasure :(
http://support.microsoft.com/kb/983097
http://support.microsoft.com/kb/2257542

Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321
  • Hey @Jeremy, i was thinking about this too, but first I wanted to see if it's really not possible with space. Looks like I will Implement Ctrl + Space if there's no other solution. I hope that the File Preview won't be too bad, when I finished it I'll publish the Sourcecode. I'm only having problems with the Preview of Microsoft Office Documents. Best Regards ans thanks for the comment. – user2974830 Feb 03 '15 at 13:19
  • There were a lot of problems - really esoteric - caused by Excel Previews at my last job and with many utilities I have used to do this in the past. Thats why I jumped in, I hope your project is a success (and I will follow your questions on this topic). Since this is your first bounty dont forget to award it by checking the bounty underneath the answer tick mark. Cheers – Jeremy Thompson Feb 04 '15 at 09:15