It works on OSX because it appears the operating system automatically creates a keypress of scancode 27 (escape) when you press "^[".
The special code in term_keypress_inner to handle this combination of keys is never hit, because no keypress event is created for ^[, but it isn't necessary on OSX due to the above feature.
You could expand your handling of keyup/keydown to handle it. For example, change your keyup handler to the following: