as key_lastpressed is read-only you will need to create a new variable for determining the new possible key presses.

you'll need to create a sort of listener to identify if the key has been pressed and then released
Code:
#define KEY_HAT 290
#define KEY_RAWX 291

var prev_joyhat = joy_hat;
VECTOR prev_joyraw;
vec_set(prev_joyraw, joy_raw);

while(1){
  if(joy_hat != prev_joyhat){
    newkey_event(KEY_HAT);
    prev_joyhat = joy_hat;
  }
  if(joy_raw.x != prev_joyraw.x){
    newkey_event(KEY_RAWX);
    prev_joyraw.x = prev_joyraw.x){
  }
//etc...
  wait(1);
}

also, should you require it, you can pass which direction was triggered too by adding additional parameters

hope this helps