Last active
December 20, 2024 06:40
-
-
Save orange-in-space/175b4b1b33aa6dafa4dbff2c1a8517cf to your computer and use it in GitHub Desktop.
ToggleCapsLock. CSharp and Windows-batch Combined file. (CapsLockを切り替えるアプリを作れるやつ><)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
@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 [93mFind csc.exe[0m | |
setlocal | |
for /f "usebackq delims=" %%A in (`dir /s /b %WINDIR%\Microsoft.NET\CSC.EXE`) do set CSCFULLPATH=%%A | |
echo [93m csc.exe fullpath is[0m %CSCFULLPATH% | |
@echo on | |
%CSCFULLPATH% /platform:x64 /target:winexe %~n0.bat | |
@if %errorlevel% equ 0 ( | |
echo [96m I think it probably compiled fine! [0m | |
echo [[94m %~n0.exe [0m] | |
) | |
@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