Do you get the whole string of the pressed key at once, like: \e\UP_ARROW_CODE? If so, you can trim the \e code and put the rest in the hash. If you get a single \e - this is your escape key, you can put it in the hash too. Can't you solve your problem with regexps?