10

the Ctrl+Backspace keyboard shortcut normally deletes the word to the left from the caret. But it does not work in any of the standard Delphi controls you would expect (TEdit, TMemo, TComboBox ect.). Interestingly, Ctrl+Delete works just fine.

Does anybody know how to correct this behaviour easily?

I know that overriding the KeyDown procedure would do it, but this is a rather too complicated as KeyDown has to be overridden for every component.

oxo
  • 946
  • 9
  • 21
  • This is not a Delphi-specific issue. Indeed, `TEdit` and the other controls are merely wrappers for the native Microsoft Windows controls, and they behave like this. Try, for instance, the edit box in Notepad's 'Select Font' dialog. Edit controls with auto-completion enabled, however, accept the Ctrl+Backspace hotkey. – Andreas Rejbrand Apr 24 '12 at 20:40
  • 3
    Like Raymond [`said`](http://blogs.msdn.com/b/oldnewthing/archive/2007/10/11/5395501.aspx). – TLama Apr 24 '12 at 20:42
  • Interesting, I didn't know that. But it is still a little bit annoying because the shortcut works at so many places including even MS software. – oxo Apr 24 '12 at 20:51
  • 1
    @oxo: I agree, this is a shortcut I really expect and use daily, in most software. Probably the standard edit box hasn't got this feature by default because of some obscure and subtle compatibility issue with old (or misbehaving) software. Still one might wonder why there isn't a style one can use to enable this feature. – Andreas Rejbrand Apr 24 '12 at 20:54
  • 1
    At the opposite side is *RichEdit control* where is this shortcut implemented by default. – TLama Apr 24 '12 at 21:00
  • @TLama: But that one lacks a built-in context menu, as discussed here a couple of days ago. It is too bad the standard controls 'lack' feature like this... – Andreas Rejbrand Apr 24 '12 at 21:01
  • I'd be tempted to shove this in my own TEdit-descendant, or into TJvEdit. – Warren P Apr 24 '12 at 22:45

1 Answers1

17

This is not a Delphi-specific issue. Indeed, TEdit and the other controls are merely wrappers for the native Microsoft Windows controls, and they behave like this. Try, for instance, the edit box in Notepad's 'Select Font' dialog. Edit controls with auto-completion enabled, however, accept the Ctrl+Backspace hotkey. I think you can enable this easily by

SHAutoComplete(Edit1.Handle,
  SHACF_AUTOAPPEND_FORCE_OFF or SHACF_AUTOSUGGEST_FORCE_OFF)

(uses ShLwApi).

Andreas Rejbrand
  • 105,602
  • 8
  • 282
  • 384
  • +1, I was writing a comment you to change the flag from `SHACF_DEFAULT = 0`, you were faster. It is the easiest way to implement this feature. – TLama Apr 24 '12 at 20:49
  • @TLama: Yeah, the actual autocompletion is probably not what the OP wants... Still, I wonder if there isn't a better way to do this. After all, it makes little sense to enable 'disabled autocompletion' in order to get the Ctrl+Backspace hotkey to work... – Andreas Rejbrand Apr 24 '12 at 20:50
  • One should probably also add the flag `SHACF_FILESYS_ONLY`, not because it makes sense, but because the documentation explicitly says "This flag must be used in combination with one or more of the SHACF_FILESYS* or SHACF_URL* flags." referring to the flags I use above. – Andreas Rejbrand Apr 24 '12 at 21:03
  • Nice trick! But, unfortunately sometimes the shortcut does not work. But it is definitely a good solution in cases where KeyDown can't be overridden. Otherwise it is possible to simulate Ctrl+Shift+Left and Delete keystrokes in KeyDown and ignore #$7F Char in KeyPress. Thanks! – oxo Apr 24 '12 at 21:21
  • The parameters do not really matter. If you replace the OFF with ON, ctrl+backspace still works. Could you help to comment? – SOUser Dec 19 '12 at 13:00
  • 5
    Also, calling SHAutoComplete makes it impossible to detect "Enter" in Edit's KeyPress event handler. Could you help to comment how to work around this? – SOUser Dec 19 '12 at 13:01
  • Also, after calling SHAutoComplete may not work correctly: TMaskEdit (deleting by ctrl+bcksp may block editing of deleted part); any DB-aware component (editing by ctrl+bkspc doesn't switch dataset into edit mode and edited value is not stored). Also TComboBox should be fixed in slightly different way. In other words it is safer don't use this function and keep default behavior of editors. – Andrei Galatyn May 04 '15 at 12:09
  • 1
    I recommend not to use `SHAutoComplete` for enabling these shortcuts. As pointed out above, several things break, including [pressing `Tab` does not do anything](https://github.com/HeidiSQL/HeidiSQL/issues/765). – Anse Oct 07 '19 at 18:15