key dectect: fix some bugs