%% hcrkey.sty
%% provides \hcrkey command
%%
%% ver0.4: pdftex support via graphics-approach (2024/10/06)
%% ver0.3: add \hcrkeys command (2022/09/12)
%% ver0.2: rename to kshcrkey (2022/07/21)
%% ver0.1: luatex support (2020/10/16)
%% ver0.0: xetex support (2020/10/15)
\ProvidesPackage{kshcrkey}[2024/10/06 v0.4.1]
\RequirePackage{xparse}
\RequirePackage{etoolbox}
\RequirePackage{iftex}
\AtEndPreamble{
	\@ifpackageloaded{graphicx}{}{\RequirePackage{graphicx}}
	\ifPDFTeX
		\@ifpackageloaded{adjustbox}{}{\RequirePackage[export]{adjustbox}}
	\else
		\@ifpackageloaded{fontspec}{}{\RequirePackage{fontspec}}
	\fi
}

\ExplSyntaxOn 
\tl_if_exist:NF \hcrkeyfontname
{
	\tl_set:Nn \hcrkeyfontname { HANBatang-LVT.ttf }
}
\prop_new:N \g_hcrkey_prop
\prop_set_from_keyval:Nn \g_hcrkey_prop
{
%%% function keys
	f1 = F035F,
	f2 = F0360,
	f3 = F0361,
	f4 = F0362,
	f5 = F0363,
	f6 = F0364,
	f7 = F0365,
	f8 = F0366,
	f9 = F0367,
	f10 = F0368,
	f11 = F0369,
	f12 = F036A,
%% alt, ctrl, shift, tab, space
	alt = F036B,
	ctrl = F036D,
	shift = F036C,
	tab = F036E,
	space = F039E,
	esc = F03A2,
%% 1st line
	backquote = F036F,
	tilde = F036F,
	! = F0370,
	1 = F0370,
	@ = F0371,
	2 = F0371,
	hash = F0372,
	3 = F0372,
	dollar = F0373,
	4 = F0373,
	percent = F0374,
	5 = F0374,
	caret = F0375,
	6 = F0375,
	ampersand = F0376,
	7 = F0376,
	* = F0377,
	8 = F0377,
	( = F0378,
	9 = F0378,
	) = F0379,
	0 = F0379,
	- = F037A,
	_ = F037A,
	+ = F037B,
	{=} = F037B,
	| = F037C,
	backslash = F037C,
%% alphabet
	a = F037D,
	b = F037E,
	c = F037F,
	d = F0380,
	e = F0381,
	f = F0382,
	g = F0383,
	h = F0384,
	i = F0385,
	j = F0386,
	k = F0387,
	l = F0388,
	m = F0389,
	n = F038A,
	o = F038B,
	p = F038C,
	q = F038D,
	r = F038E,
	s = F038F,
	t = F0390,
	u = F0391,
	v = F0392,
	w = F0393,
	x = F0394,
	y = F0395,
	z = F0396,
%% pucn keys
 	\{ = F0397,
	leftbrace = F0397,
	[ = F0397,
	\} = F0398,
	rightbrace = F0398,
	] = F0398,
	: = F0399,
	; = F0399,
	" = F039A,
	' = F039A,
	< = F039B,
	{,} = F039B,
	comma = F039B,
	> = F039C,
	. = F039C,
	? = F039D,
	/ = F039D,
	space = F039E,
	backspace = F03A1,
	enter = F03A0,
%%% lock keys	
	capslock = F039F,
	numlock = F03A3,
	scrolllock = F03A4,
	sysreq = F03A5,
%% numpad keys
	numprtsc = F03A6,
	num+ = F03A8,
	num- = F03A7,
	numdel = F03A9,
	numdot = F03A9,
	numins = F03AA,
	num0 = F03AA,
	numend = F03AB,
	num1 = F03AB,
	numdown = F03AC,
	num2 = F03AC,
	numpgdn = F03AD,
	num3 = F03AD,
	numleft = F03AE,
	num4 = F03AE,
	num5 = F03AF,
	numright = F03B0,
	num6 = F03B0,
	numhome = F03B1,
	num7 = F03B1,
	numup = F03B2,
	num8 = F03B2,
	numpgup = F03B3,
	num9 = F03B3,
	numenter = F03C0,
	num* = F03C1,
	num/ = F03C2,
	prtsc = F03C3,
	numpause = F03C4,
	pause = F03C4,
%% cursor keys
	insert = F03B6,
	ins = F03B6,
	delete = F03B7,
	del = F03B7,
	home = F03B8,
	end = F03B9,
	pageup = F03BA,
	pgup = F03BA,
	pagedown = F03BB,
	pgdn = F03BB,
	left = F03BC,
	right = F03BD,
	up = F03BE,
	down = F03BF,
%% 기타
	blank = F03C5,
}

\NewDocumentCommand \hcrkey_unicode { m }
{
	\tl_set:Nx \l_tmpa_tl { \str_foldcase:n { #1 } }
	\str_case_e:nnF { \l_tmpa_tl } 
	{
		{ windows }
		  {
    		\group_begin:
    		\fontspec {\hcrkeyfontname} 
    		\raisebox{-.09em}{\includegraphics[width=.9em]{hcrkey_winlogo}\hspace{.05em}}
			\sys_if_engine_luatex:TF 
			{
				\hbox_overlap_left:n { \symbol { "F03C7 } }
			}
			{
	    		\hbox_overlap_left:n { \symbol{"F03C5 } }
			}
    		\group_end:
		  }
		{ fn }  
		  {
        	\group_begin:
        	\fontspec { \hcrkeyfontname }
        	\hbox_set:Nn \l_tmpa_box { \sffamily Fn \hspace{.1em} }
        	\box_resize_to_wd:Nn \l_tmpa_box { .9em }
        	\box_use_drop:N \l_tmpa_box 
			\sys_if_engine_luatex:TF
			{
				\hbox_overlap_left:n { \symbol { "F03C7 } }
			}
			{
	        	\hbox_overlap_left:n { \symbol{"F03C5 } }
			}
        	\group_end:
		  }
	}
	{
    	\prop_get:NVN \g_hcrkey_prop \l_tmpa_tl \l_tmpb_tl
%% luatex: advancing by 2 needed. why?  
    	\sys_if_engine_luatex:T 
		  { 
		    \exp_args:NNx \int_set:Nn \l_tmpa_int { \exp_args:No \int_from_hex:n { \l_tmpb_tl } }
		    \int_add:Nn \l_tmpa_int { 2 }
		    \tl_set:Nx \l_tmpb_tl { \exp_args:No \int_to_Hex:n { \l_tmpa_int } }
		  }
%%%%%%%
    	\quark_if_no_value:NF \l_tmpb_tl
    	  {
        	  \group_begin: 
        		\exp_args:No \fontspec {\hcrkeyfontname}
        		\exp_args:Nx \symbol { " \l_tmpb_tl }
        	  \group_end:
    	  }
	 }
}

\NewDocumentCommand \hcrkey_legacy { m }
{
	\tl_set:Nx \l_tmpa_tl { \str_foldcase:n { #1 } }
	\str_case_e:nnF { \l_tmpa_tl } 
	{
		{ windows }
		  {
    		\group_begin:
    		\raisebox{-.09em}{ \raisebox{-.1em}{\includegraphics[width=.9em]{hcrkey_winlogo}\hspace{.05em}}}
	    	\hbox_overlap_left:n { \raisebox{-.3em}{\includegraphics [height=1em] {F03C5} } }
    		\group_end:
		  }
		{ fn }  
		  {
        	\group_begin:
        	\hbox_set:Nn \l_tmpa_box { \sffamily Fn \hspace{.1em} }
        	\box_resize_to_wd:Nn \l_tmpa_box { .9em }
        	\box_use_drop:N \l_tmpa_box 
	        \hbox_overlap_left:n { \raisebox{-.3em}{\includegraphics [height=1em] {F03C5}} }
        	\group_end:
		  }
	}
	{
    	\prop_get:NVN \g_hcrkey_prop \l_tmpa_tl \l_tmpb_tl
%%%%%%%
    	\quark_if_no_value:NF \l_tmpb_tl
    	  {
        	  \group_begin: 
%        		\exp_args:No \fontspec {\hcrkeyfontname}
        		\raisebox{-.3em}{\includegraphics [height=1em] { \l_tmpb_tl }}
        	  \group_end:
    	  }
	 }
}

\sys_if_engine_pdftex:TF
{
	\let\hcrkey\hcrkey_legacy
}
{
	\let\hcrkey\hcrkey_unicode
}

\NewDocumentCommand \hcrkeys { O{\space} m }
{
	\seq_set_split:Nnn \l_kskeys_seq {, } { #2 }
	\seq_map_indexed_inline:Nn \l_kskeys_seq
	{
		\hcrkey{##2}
		\int_compare:nT { ##1 < \seq_count:N \l_kskeys_seq }
		{ #1 }
	}
}


\ExplSyntaxOff

\endinput
