Prevent some key presses from inputting to text field

Sorry if this has been posted before, but I couldn’t find anything.

I want to limit text entry into a text field to just the delete key, all other key presses should be ignored. I’ve tried various strategies with “keyboard down”, “should begin editing” and “should end editing” but the logic is confounding me. I merely want to prevent the user from entering anything (from the keyboard–the app uses a popup menu to insert other text into the text field) but DELETE.

How would I build such a filter?

TIA, as always.

Hi Doug,

I wonder if filerting the input is necessacy at all? Maybe you simply want to clear the text field when the text field is selected and the delete key is hit? You could try this then:

  • set your text field to not “Editable” in Interface Builder
  • set your Text field to “Selectable” in IB

then connect it to the ‘on keyboard up’ handler:

on keyboard up theObject event theEvent
	if (get key code of theEvent) = 51 then
		set content of theObject to ""
	end if
end keyboard up

D.