I think that key values are not the same as the ASCII character value. In other words, $keyval does not return an ascii code, but instead a different number identifying the key.