I was thinking more along the lines of a door-locking widget you can carry around, just to make it more obvious for the player.
The script could get quite complex. You'd have to check if the tag of the key matches the key tag of the door you targeted, via GetLockKeyTag(). It'd be easier to give the key itself a unique power (most likely touch-ranged), that way you don't have to search the inventory for the presence of a specific key (just use the tag of the activated item).
And would you want to check whether the targeted door was closed before locking it, or would you automatically want the script to close it *while* locking it? The former is more realistic, but the latter might be more player-friendly.
Another option is to give a locked door a heartbeat script so that it closes and re-locks itself after it's been opened. You'd want to check that the last opener was no longer within a certain range, to stop it from closing in the player's face if they hesitate. I have an automatic door-closing script that does much the same (albeit without any re-locking function):
// hb_door_autoclose
//
// Auto-close a door if the last opener is no longer within 5m of it
void main()
{
object oDoor = OBJECT_SELF;
object oUser = GetLastUsedBy();
int iStayOpen = 0;
if (GetIsOpen(oDoor) == FALSE) return;// Do nothing if door is not open
object oTarget = GetFirstObjectInShape(SHAPE_SPHERE, 5.0, GetLocation(oDoor), FALSE, OBJECT_TYPE_CREATURE);
while (GetIsObjectValid(oTarget))
{
if (oTarget == oUser) iStayOpen = 1;// Don't close if last opener still close
oTarget = GetNextObjectInShape(SHAPE_SPHERE, 5.0, GetLocation(oDoor), FALSE, OBJECT_TYPE_CREATURE);
}//end while
if (iStayOpen) return;// Abort closing
AssignCommand(oDoor, ActionCloseDoor(oDoor));
}