TmKeyRec:bug fix & refactoring