Console live toggle Quick Edit with these one-liner functions :QuickEditOFF :QuickEditON :PrintQuickEdit

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
shodan
Posts: 89
Joined: 01 May 2023 01:49

Console live toggle Quick Edit with these one-liner functions :QuickEditOFF :QuickEditON :PrintQuickEdit

#1 Post by shodan » 20 May 2024 01:46

Quick and self-explanatory

Code: Select all

:QuickEditOFF
powershell -command "Add-Type -TypeDefinition 'using System;using System.Runtime.InteropServices;public class QEOFF{[DllImport(\"kernel32.dll\")]private static extern IntPtr GetStdHandle(int nStdHandle);[DllImport(\"kernel32.dll\")]private static extern bool GetConsoleMode(IntPtr hConsoleHandle,out uint lpMode);[DllImport(\"kernel32.dll\")]private static extern bool SetConsoleMode(IntPtr hConsoleHandle, uint dwMode);public static void QuickEditOFF(){IntPtr hConIn=GetStdHandle(-10);uint dwOldMode;if(GetConsoleMode(hConIn,out dwOldMode)){SetConsoleMode(hConIn,(uint)((dwOldMode|0x80)&~0x40));}}}';[QEOFF]::QuickEditOFF()"
GoTo :EOF

Code: Select all

:QuickEditON
powershell -command "Add-Type -TypeDefinition 'using System;using System.Runtime.InteropServices;public class QEON{[DllImport(\"kernel32.dll\")]private static extern IntPtr GetStdHandle(int nStdHandle);[DllImport(\"kernel32.dll\")]private static extern bool GetConsoleMode(IntPtr hConsoleHandle,out uint lpMode);[DllImport(\"kernel32.dll\")]private static extern bool SetConsoleMode(IntPtr hConsoleHandle, uint dwMode);  public static void QuickEditON(){IntPtr hConIn=GetStdHandle(-10);uint dwOldMode;if(GetConsoleMode(hConIn,out dwOldMode)){SetConsoleMode(hConIn,dwOldMode|0xC0);}}}';[QEON]::QuickEditON()"
GoTo :EOF
This PrintQuickEdit is currently broken in a very strange way
It will always turn off Quick Edit, even though, it simply cannot
I don't understand how this is possible
Caching executable assembly not getting recompiled perhaps ?

Code: Select all

:PrintQuickEdit
powershell -command "Add-Type -TypeDefinition 'using System;using System.Runtime.InteropServices;public class QuickEditStatus{[DllImport(\"kernel32.dll\")]private static extern IntPtr GetStdHandle(int nStdHandle);[DllImport(\"kernel32.dll\")]private static extern bool GetConsoleMode(IntPtr hConsoleHandle,out uint lpMode);public static void PrintQuickEdit(){IntPtr hConIn=GetStdHandle(-10);uint dwOldMode;if(GetConsoleMode(hConIn,out dwOldMode)){if((dwOldMode & 0x40) != 0){System.Console.WriteLine(\"quickedit=true\");System.Environment.Exit(0);}else{System.Console.WriteLine(\"quickedit=false\");System.Environment.Exit(1);}}else{System.Console.WriteLine(\"Failed to get console mode.\");System.Environment.Exit(1);}}}';[QuickEditStatus]::PrintQuickEdit()"
GoTo :EOF

Here is the code re-expanded with AI, this may have introduced errors

:QuickEditOFF

Code: Select all

powershell -command "
Add-Type -TypeDefinition '
using System;
using System.Runtime.InteropServices;

public class QEOFF {
    [DllImport(\"kernel32.dll\")]
    private static extern IntPtr GetStdHandle(int nStdHandle);

    [DllImport(\"kernel32.dll\")]
    private static extern bool GetConsoleMode(IntPtr hConsoleHandle, out uint lpMode);

    [DllImport(\"kernel32.dll\")]
    private static extern bool SetConsoleMode(IntPtr hConsoleHandle, uint dwMode);

    // Method to turn off Quick Edit mode
    public static void QuickEditOFF() {
        IntPtr hConIn = GetStdHandle(-10); // Get handle to the standard input
        uint dwOldMode;
        
        if (GetConsoleMode(hConIn, out dwOldMode)) {
            // Disable Quick Edit mode by modifying console mode flags
            SetConsoleMode(hConIn, (uint)((dwOldMode | 0x80) & ~0x40));
        }
    }
}';

[QEOFF]::QuickEditOFF()
"
:QuickEditON

Code: Select all

powershell -command "
Add-Type -TypeDefinition '
using System;
using System.Runtime.InteropServices;

public class QEON {
    [DllImport(\"kernel32.dll\")]
    private static extern IntPtr GetStdHandle(int nStdHandle);

    [DllImport(\"kernel32.dll\")]
    private static extern bool GetConsoleMode(IntPtr hConsoleHandle, out uint lpMode);

    [DllImport(\"kernel32.dll\")]
    private static extern bool SetConsoleMode(IntPtr hConsoleHandle, uint dwMode);

    // Method to turn on Quick Edit mode
    public static void QuickEditON() {
        IntPtr hConIn = GetStdHandle(-10); // Get handle to the standard input
        uint dwOldMode;

        if (GetConsoleMode(hConIn, out dwOldMode)) {
            // Enable Quick Edit mode by modifying console mode flags
            SetConsoleMode(hConIn, dwOldMode | 0xC0);
        }
    }
}';

[QEON]::QuickEditON()
"
:PrintQuickEdit

Code: Select all

powershell -command "
Add-Type -TypeDefinition '
using System;
using System.Runtime.InteropServices;

public class QuickEditStatus {
    [DllImport(\"kernel32.dll\")]
    private static extern IntPtr GetStdHandle(int nStdHandle);

    [DllImport(\"kernel32.dll\")]
    private static extern bool GetConsoleMode(IntPtr hConsoleHandle, out uint lpMode);

    // Method to print the Quick Edit mode status
    public static void PrintQuickEdit() {
        IntPtr hConIn = GetStdHandle(-10); // Get handle to the standard input
        uint dwOldMode;

        if (GetConsoleMode(hConIn, out dwOldMode)) {
            if ((dwOldMode & 0x40) != 0) {
                System.Console.WriteLine(\"quickedit=true\");
                System.Environment.Exit(0); // Exit with code 0 (true)
            } else {
                System.Console.WriteLine(\"quickedit=false\");
                System.Environment.Exit(1); // Exit with code 1 (false)
            }
        } else {
            System.Console.WriteLine(\"Failed to get console mode.\");
            System.Environment.Exit(1); // Exit with code 1 (failure)
        }
    }
}';

[QuickEditStatus]::PrintQuickEdit()
"

Post Reply