Skip to content

Instantly share code, notes, and snippets.

@orange-in-space
Last active December 20, 2024 06:40
Show Gist options
  • Save orange-in-space/175b4b1b33aa6dafa4dbff2c1a8517cf to your computer and use it in GitHub Desktop.
Save orange-in-space/175b4b1b33aa6dafa4dbff2c1a8517cf to your computer and use it in GitHub Desktop.
ToggleCapsLock. CSharp and Windows-batch Combined file. (CapsLockを切り替えるアプリを作れるやつ><)
/*
@echo off
@goto bat_part
ToggleCapsLock. CSharp and Windows-batch Combined file.
(CapsLockを切り替えるアプリを作れるやつ><)
Please be sure to set line breaks
to CR+LF before executing this batch file!
This file is both a batch file and C# source code,
and can locate csc.exe and compile itself.
In principle, an error is inevitably displayed
at the execution of the first line at compile time,
but do not worry about it!
Running the generated ToggleCapsLock.exe should toggle CapsLock...
in Japanese description
実行前に必ず改行コードをCR+LFにしてください!!!><
このファイルは、バッチファイル兼C#のソースコードになっていて、
実行するとcsc.exeを探し出して自らをコンパイルします><
原理上、どうしてもコンパイル時の一行目の実行時にエラーが表示されますが、気にしないでください><;
出来上がったToggleCapsLock.exeを実行するとCapsLockが切り替わるはず><
:bat_part
echo ----
echo Find csc.exe
setlocal
for /f "usebackq delims=" %%A in (`dir /s /b %WINDIR%\Microsoft.NET\CSC.EXE`) do set CSCFULLPATH=%%A
echo  csc.exe fullpath is %CSCFULLPATH%
@echo on
%CSCFULLPATH% /platform:x64 /target:winexe %~n0.bat
@if %errorlevel% equ 0 (
echo  I think it probably compiled fine! 
echo [ %~n0.exe ]
)
@PAUSE
@goto :eof
*/
// cs part
using System;
using System.Runtime.InteropServices;
namespace SendCapsLock
{
class Program
{
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
public static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int extraInfo);
private const int VK_CAPITAL = 0x14; // CAPS LOCK
private const int KEYEVENTF_EXTENDEDKEY = 0x1;
private const int KEYEVENTF_KEYUP = 0x2;
static void Main()
{
ToggleCapsLock();
}
private static void ToggleCapsLock()
{
keybd_event(VK_CAPITAL, 0x45, KEYEVENTF_EXTENDEDKEY, 0);
keybd_event(VK_CAPITAL, 0x45, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment