Batch file to run keyboard commands

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
z4rek
Posts: 1
Joined: 01 Oct 2017 11:13

Batch file to run keyboard commands

#1 Post by z4rek » 01 Oct 2017 11:15

I am trying to run a command to active as soon as I log on to my laptop. I believe I might have to make a batch file (or something), and run it that way. But basically, when I turn on my laptop, I want the backlight on my keyboard to automatically turn off (there are no native settings to do this).

therefore, is it possible to write a command to do the following:

Press FN + F4

Then press FN + F3

Then press FN + F3

Then press FN + F3

Thanks

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Batch file to run keyboard commands

#2 Post by aGerman » 02 Oct 2017 04:57

Batch can't emulate any keyboard input. But even if it could, the Fn key is hard-wired and thus, it's not accessible by a script. You would have to write your own keyboard driver. Not worth the efforts compared to the costs of another keyboard without backlight.

Steffen

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Batch file to run keyboard commands

#3 Post by penpen » 02 Oct 2017 13:29

One cannot emulate the pressing of the "Fn"-key on any system, but under a specific circumstance you could emulate the result:
If turning off the backlight on your keyboard is not itself hard wired, but handled by software, then a (virtual) key (!= "F1") is pressed.

In that case and if .NET is not disabled on your system, then you could use the below "ReadKey.cs.bat" to retrieve the key name, the (virtual) keycode and its C++ definition. After that it should be possible to use AutoIt to do wht you want:
https://www.autoitscript.com/site/autoit/.

Code: Select all

Z:\>ReadKey.cs.bat
most recent C#.NET compiler located in:
"C:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe".

Z:\>ReadKey.exe -VK
Enter
0x0D: VK_RETURN

Z:\>ReadKey.exe -VK
VolumeMute
0xAD: VK_VOLUME_MUTE

Z:\>


"Readkey.cs.bat":

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 GetAsyncKeyState(int keyCode);

   [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true, CallingConvention = CallingConvention.Winapi)]
   public static extern short GetKeyState(int keyCode);


   /*
    * Virtual Key codes:
    * https://msdn.microsoft.com/en-us/library/windows/desktop/dd375731(v=vs.85).aspx
    */
   public static string[] nVirtKeyName = {
//      _0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _A, _B, _C, _D, _E, _F
/* 0_ */   "0x00 Undefined", "VK_LBUTTON", "VK_RBUTTON", "VK_CANCEL", "VK_MBUTTON", "VK_XBUTTON1", "VK_XBUTTON2", "- 0x07 Undefined", "VK_BACK", "VK_TAB", "- 0x0A Reserved", "- 0x0B Reserved", "VK_CLEAR", "VK_RETURN", "- 0x0E Undefined", "- 0x0F Undefined",
/* 1_ */   "VK_SHIFT", "VK_CONTROL", "VK_MENU", "VK_PAUSE", "VK_CAPITAL", "VK_KANA/VK_HANGUEL/VK_HANGUL", "- 0x16 Undefined", "VK_JUNJA", "VK_FINAL", "VK_HANJA/VK_KANJI", "- 0x1A Undefined", "VK_ESCAPE", "VK_CONVERT", "VK_NONCONVERT", "VK_ACCEPT", "VK_MODECHANGE",
/* 2_ */   "VK_SPACE", "VK_PRIOR", "VK_NEXT", "VK_END", "VK_HOME", "VK_LEFT", "VK_UP", "VK_RIGHT", "VK_DOWN", "VK_SELECT", "VK_PRINT", "VK_EXECUTE", "VK_SNAPSHOT", "VK_INSERT", "VK_DELETE", "VK_HELP",
/* 3_ */   "VK_KEY_0", "VK_KEY_1", "VK_KEY_2", "VK_KEY_3", "VK_KEY_4", "VK_KEY_5", "VK_KEY_6", "VK_KEY_7", "VK_KEY_8", "VK_KEY_9", "- 0x3A Undefined", "- 0x3B Undefined", "- 0x3C Undefined", "- 0x3D Undefined", "- 0x3E Undefined", "- 0x3F Undefined",
/* 4_ */   "- 0x40 Undefined", "VK_KEY_A", "VK_KEY_B", "VK_KEY_C", "VK_KEY_D", "VK_KEY_E", "VK_KEY_F", "VK_KEY_G", "VK_KEY_H", "VK_KEY_I", "VK_KEY_J", "VK_KEY_K", "VK_KEY_L", "VK_KEY_M", "VK_KEY_N", "VK_KEY_O",
/* 5_ */   "VK_KEY_P", "VK_KEY_Q", "VK_KEY_R", "VK_KEY_S", "VK_KEY_T", "VK_KEY_U", "VK_KEY_V", "VK_KEY_W", "VK_KEY_X", "VK_KEY_Y", "VK_KEY_Z", "VK_LWIN", "VK_RWIN", "VK_APPS", "- 0x5E Reserved", "VK_SLEEP",
/* 6_ */   "VK_NUMPAD0", "VK_NUMPAD1", "VK_NUMPAD2", "VK_NUMPAD3", "VK_NUMPAD4", "VK_NUMPAD5", "VK_NUMPAD6", "VK_NUMPAD7", "VK_NUMPAD8", "VK_NUMPAD9", "VK_MULTIPLY", "VK_ADD", "VK_SEPARATOR", "VK_SUBTRACT", "VK_DECIMAL", "VK_DIVIDE",
/* 7_ */   "VK_F1", "VK_F2", "VK_F3", "VK_F4", "VK_F5", "VK_F6", "VK_F7", "VK_F8", "VK_F9", "VK_F10", "VK_F11", "VK_F12", "VK_F13", "VK_F14", "VK_F15", "VK_F16",
/* 8_ */   "VK_F17", "VK_F18", "VK_F19", "VK_F20", "VK_F21", "VK_F22", "VK_F23", "VK_F24", "- 0x88 Unassigned", "- 0x89 Unassigned", "- 0x8A Unassigned", "- 0x8B Unassigned", "- 0x8C Unassigned", "- 0x8D Unassigned", "- 0x8E Unassigned", "- 0x8F Unassigned",
/* 9_ */   "VK_NUMLOCK", "VK_SCROLL", "0x92 OEM specific", "0x93 OEM specific", "0x94 OEM specific", "0x95 OEM specific", "0x96 OEM specific", "- 0x97 Unassigned", "- 0x98 Unassigned", "- 0x99 Unassigned", "- 0x9A Unassigned", "- 0x9B Unassigned", "- 0x9C Unassigned", "- 0x9D Unassigned", "- 0x9E Unassigned", "- 0x9F Unassigned",
/* A_ */   "VK_LSHIFT", "VK_RSHIFT", "VK_LCONTROL", "VK_RCONTROL", "VK_LMENU", "VK_RMENU", "VK_BROWSER_BACK", "VK_BROWSER_FORWARD", "VK_BROWSER_REFRESH", "VK_BROWSER_STOP", "VK_BROWSER_SEARCH", "VK_BROWSER_FAVORITES", "VK_BROWSER_HOME", "VK_VOLUME_MUTE", "VK_VOLUME_DOWN", "VK_VOLUME_UP",
/* B_ */   "VK_MEDIA_NEXT_TRACK", "VK_MEDIA_PREV_TRACK", "VK_MEDIA_STOP", "VK_MEDIA_PLAY_PAUSE", "VK_LAUNCH_MAIL", "VK_LAUNCH_MEDIA_SELECT", "VK_LAUNCH_APP1", "VK_LAUNCH_APP2", "- 0xB8 Reserved", "- 0xB9 Reserved", "VK_OEM_1", "VK_OEM_PLUS", "VK_OEM_COMMA", "VK_OEM_MINUS", "VK_OEM_PERIOD", "VK_OEM_2",
/* C_ */   "VK_OEM_3", "- 0xC1 Reserved", "- 0xC2 Reserved", "- 0xC3 Reserved", "- 0xC4 Reserved", "- 0xC5 Reserved", "- 0xC6 Reserved", "- 0xC7 Reserved", "- 0xC8 Reserved", "- 0xC9 Reserved", "- 0xCA Reserved", "- 0xCB Reserved", "- 0xCC Reserved", "- 0xCD Reserved", "- 0xCE Reserved", "- 0xCF Reserved",
/* D_ */   "- 0xD0 Reserved", "- 0xD1 Reserved", "- 0xD2 Reserved", "- 0xD3 Reserved", "- 0xD4 Reserved", "- 0xD5 Reserved", "- 0xD6 Reserved", "- 0xD7 Reserved", "- 0xD8 Unassigned", "- 0xD9 Unassigned", "- 0xDA Unassigned", "VK_OEM_4", "VK_OEM_5", "VK_OEM_6", "VK_OEM_7", "VK_OEM_8",
/* E_ */   "- 0xE0 Reserved", "0xE1 OEM specific", "VK_OEM_102", "0xE3 OEM specific", "0xE4 OEM specific", "VK_PROCESSKEY", "0xE6 OEM specific", "VK_PACKET", "- 0xE8 Unassigned", "0xE9 OEM specific", "0xEA OEM specific", "0xEB OEM specific", "0xEC OEM specific", "0xED OEM specific", "0xEE OEM specific", "0xEF OEM specific",
/* F_ */   "0xF0 OEM specific", "0xF1 OEM specific", "0xF2 OEM specific", "0xF3 OEM specific", "0xF4 OEM specific", "0xF5 OEM specific", "VK_ATTN", "VK_CRSEL", "VK_EXSEL", "VK_EREOF", "VK_PLAY", "VK_ZOOM", "VK_NONAME", "VK_PA1", "VK_OEM_CLEAR", "- 0xFF Undefined"
   };

   public static void Main (string[] args) {
      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 ());

      if (args.Length > 0) {
         if (args[0].Equals("-VK")) {
            Console.WriteLine ("0x{0,2:X2}: {1}", (int) cki.Key, nVirtKeyName[(int) cki.Key]);
         }
      }
   }
}


penpen

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Batch file to run keyboard commands

#4 Post by aGerman » 02 Oct 2017 18:00

Interesting piece of code :) Although I'm still afraid that something like "turn off backlight" is too specific. I've got a Fn+F5 and Fn+F6 shortcut on my keyboard to change the brightness of the display. No VK codes catched using your tool.

Steffen

Post Reply