KEYS changes its state even if it's called within brackets or in conditional execution statements and to child threads/processes spawned with pipes or FOR (unlike ECHO):
Code: Select all
@echo off
setlocal disableDelayedExpansion
set "if_on=for /f "tokens=3" %%# in ('keys') do if %%#==on. "
set "if_off=for /f "tokens=3" %%# in ('keys') do if %%#==off. "
rem :: echo state cannot be examined so easy
::for /f "tokens=3" %%# in ('echo') do echo %%#
::echo|for /f "tokens=3" %%# in ('more') do echo %%#
::echo|find "on."
(
keys on
%if_on% echo Should be displayed
%if_off% echo Should be not
keys off
%if_off% echo Should be displayed
%if_on% echo Should be not
(
keys on
)
%if_on% echo Should be displayed
%if_off% echo Should be not
)
For sure this is slower than IF DEFINED (which also works in brackets context)
KEYS state can be examined also with pipes and FIND/FINDSTR - but probably will be slower.
The only case where this eventually (but probably will be never needed) can be easier for use is if IF DEFINED is piped. Rather is just