How to make redirected player movement respond to same key?

I have a 2d game (Godot 4) with different rooms with portals to go between them, and some portals redirect the direction of the player while still holding the same key. I have that working so far, but when I release the key, it keeps moving in the redirected direction. How can I make sure the player stops when it's supposed to?

Movement:

if Input.is_action_just_pressed("left"):
velocity.x = -1
$Sprite2D.flip_h = false elif Input.is_action_just_pressed("right"): velocity.x = 1$Sprite2D.flip_h = true
if Input.is_action_just_released("left"):
velocity.x = max(velocity.x,0)
if Input.is_action_just_released("right"):
velocity.x = min(velocity.x,0)

if Input.is_action_just_pressed("up"):
velocity.y = -1
elif Input.is_action_just_pressed("down"):
velocity.y = 1
if Input.is_action_just_released("up"):
velocity.y = max(velocity.y,0)
if Input.is_action_just_released("down"):
velocity.y = min(velocity.y,0)


Warp between rooms:

func warp(real_dir,dir): # ULDR
var odd = dir % 2 == 1

var vpos = position
var off = -2*MARGIN
var offw = SIZE - 2*MARGIN

var diff = player.position - (position + Vector2(SIZE/2,SIZE/2))
player.position = diff.rotated((real_dir - dir) * PI/2) + position + Vector2(SIZE/2,SIZE/2)

if dir >= 2:
off = SIZE + 2*MARGIN
offw = 2*MARGIN

if odd:
vpos = Vector2(vpos.y,vpos.x)
player.position = Vector2(player.position.y,player.position.x)

if warps[dir] == "":
player.position.y = vpos.y + off
else:
var level_pos = locate_level(warps[dir])
if odd:
level_pos = Vector2(level_pos.y,level_pos.x)
player.position.x += level_pos.x - vpos.x
player.position.y = level_pos.y + offw

if odd:
player.position = Vector2(player.position.y,player.position.x)

player.velocity = player.velocity.rotated((real_dir - dir) * PI/2)

player_inside = not warps[dir] == name


• To be clear, if the player is holding "Right" when the transition in your diagram happens, which of these should happen? * Character stops until new input * Character moves right on screen (effectively taking a 90 degree turn after passing the portal) * Character continues to move forwards (up) even though the right key is being held (effectively retaining the old bindings relative to character orientation, rather than ones for the new screen orientation) [possibly only until the input is released] Nov 13, 2023 at 22:03
• The last one. The bindings are retained while the movement is rotated. Also, @thirteen 's solution worked, but I'll also accept anything more efficient. Nov 14, 2023 at 1:41