This is no batch solution:
It needs .NET (actually i don't know the minimum version), it reads keys instead of characters (for example try type in the '!' char),
and it is only tested under XP (getKeyState function: "SCROLL LOCK" state), but worked nicely for me:
Code: Select all
// // >nul 2> nul & @goto :main
/*
:main
@echo off
setlocal
cls
set "csc="
pushd "%SystemRoot%\Microsoft.NET\Framework"
for /f "tokens=* delims=" %%i in ('dir /b /o:n "v*"') do (
dir /a-d /b "%%~fi\csc.exe" >nul 2>&1 && set "csc="%%~fi\csc.exe""
)
popd
if defined csc (
echo most recent C#.NET compiler located in:
echo %csc%.
) else (
echo C#.NET compiler not found.
goto :eof
)
%csc% /nologo /optimize /warnaserror /nowin32manifest /debug- /target:exe /out:"%~dp0readKey.exe" "%~f0"
goto :eof
*/
using System;
using System.Runtime.InteropServices;
class ReadKey {
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true, CallingConvention = CallingConvention.Winapi)]
public static extern short GetKeyState(int keyCode);
public static void Main () {
ConsoleKeyInfo cki;
Console.TreatControlCAsInput = true;
cki = Console.ReadKey (true);
if (Console.NumberLock) Console.Write ("NUM+");
if (Console.CapsLock ) Console.Write ("CAPS+");
if ((((ushort)GetKeyState (0x91)) & 0xFFFF) != 0) Console.Write ("SCROLL+");
if ((cki.Modifiers & ConsoleModifiers.Alt ) != 0) Console.Write ("ALT+");
if ((cki.Modifiers & ConsoleModifiers.Control) != 0) Console.Write ("CTRL+");
if ((cki.Modifiers & ConsoleModifiers.Shift ) != 0) Console.Write ("SHIFT+");
Console.WriteLine (cki.Key.ToString ());
}
}
penpen