123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263(* Keyboard shortcuts for the main loop *)openTsdlmoduleIntPairs=structtypet=int*intletcompare(x0,y0)(x1,y1)=matchcomparex0x1with|0->comparey0y1|c->cendmodulePairsMap=Map.Make(IntPairs)type'aaction='a->unittype'at=('aaction)PairsMap.t(* Bind a new action to the keycode. If the keycode was already present, the
previous action is disregarded. *)letadd_mapmap(keycode,keymod,action)=PairsMap.add(keycode,keymod)actionmapletremove=PairsMap.remove(* Some seemingly inocuous modifiers may be pressed, for instance Num_lock, so
we need to check against the "useful" modifiers. See
https://stackoverflow.com/questions/48706762/sdl-2-how-to-check-for-no-modifiers-keyboard-input
*)letmod_none=Sdl.Kmod.(ctrllorshiftloraltlorgui)(* Return the action bound to the keycode, or None. *)letfindmappair=matchPairsMap.find_optpairmapwith|Somer->Somer|None->letc,m=pairinPairsMap.find_opt(c,mlandmod_none)map(* Add new entries from a list of triples (keycode, keymod, action) *)letadd_listmapalist=List.fold_leftadd_mapmapalist(* Create a Shortcut map for a list of triples (keycode, keymod, action). *)letcreatealist:'at=add_listPairsMap.emptyalistletempty()=create[](* add a binding to a keycode without modifier *)letadd(keycode,action)map=add_mapmap(keycode,Sdl.Kmod.none,action)letadd_ctrl(keycode,action)map=letalist=[keycode,Sdl.Kmod.lctrl,action;keycode,Sdl.Kmod.rctrl,action]inadd_listmapalistletadd_ctrl_shift(keycode,action)map=letalist=[keycode,Sdl.Kmod.lctrllorSdl.Kmod.lshift,action;keycode,Sdl.Kmod.lctrllorSdl.Kmod.rshift,action;keycode,Sdl.Kmod.rctrllorSdl.Kmod.rshift,action;keycode,Sdl.Kmod.rctrllorSdl.Kmod.lshift,action]inadd_listmapalist