disable backspace/del?

Just starting out? Need help? Post your questions and find answers here.
User avatar
mk-soft
Always Here
Always Here
Posts: 5335
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: disable backspace/del?

Post by mk-soft »

The keys do not go to Windows, but to the edit control.
You have to process the keys that go to the edit control.

Code: Select all

;-TOP

; Comment : Module SetGadgetCallback (Windows Only)
; Author  : mk-soft
; Version : v0.02
; Created : 10.06.2018
; Updated : 
;
; Syntax Callback:
;           Procedure GadgetCB(hWnd,uMsg,wParam,lParam)
;             Select uMsg
;               ;TODO
;             EndSelect
;             ; Call previous gadget procedure
;             ProcedureReturn CallGadgetProc(hWnd,uMsg,wParam,lParam)
;           EndProcedure
;
; *****************************************************************************

DeclareModule GadgetCallback
  
  Declare SetGadgetCallback(Gadget, *lpNewFunc) 
  Declare CallGadgetProc(hWnd, uMsg, wParam, lParam)
  
EndDeclareModule

Module GadgetCallback
  
  EnableExplicit
  
  Global NewMap *lpPrevFunc()
  Global MutexCB = CreateMutex()
  
  ; ---------------------------------------------------------------------------
  
  Procedure SetGadgetCallback(Gadget, *lpNewFunc)
    Protected GadgetID, GadgetKey.s
    
    GadgetID = GadgetID(Gadget)
    GadgetKey = Hex(GadgetID)
    
    ; Remove exists Callback
    If FindMapElement(*lpPrevFunc(), GadgetKey)
      SetWindowLongPtr_(GadgetID, #GWL_WNDPROC, *lpPrevFunc())
      DeleteMapElement(*lpPrevFunc())
    EndIf
    
    If *lpNewFunc
      If AddMapElement(*lpPrevFunc(), GadgetKey)
        *lpPrevFunc() = SetWindowLongPtr_(GadgetID, #GWL_WNDPROC, *lpNewFunc)
        ProcedureReturn *lpPrevFunc()
      EndIf
    EndIf
    
    ProcedureReturn 0
    
  EndProcedure
  
  ; ---------------------------------------------------------------------------
  
  Procedure CallGadgetProc(hWnd, uMsg, wParam, lParam)
    Protected result
    
    LockMutex(MutexCB)
    If FindMapElement(*lpPrevFunc(), Hex(hWnd))
      result = CallWindowProc_(*lpPrevFunc(), hWnd, uMsg, wParam, lParam)
    EndIf
    UnlockMutex(MutexCB)
    
    ProcedureReturn result
    
  EndProcedure
  
EndModule

; *****************************************************************************

; Example

CompilerIf #PB_Compiler_IsMainFile
  
  UseModule GadgetCallback
  
  Procedure RichEditProc(hWnd,uMsg,wParam,lParam)
    Select uMsg
      Case #WM_KEYDOWN
        Select wParam
          Case #VK_DELETE
            ProcedureReturn 1
          Case #VK_BACK
            ProcedureReturn 1
        EndSelect
        
      Case #WM_CHAR
        Select wParam
          Case #VK_TAB
            SetFocus_(GetWindow_(hWnd,#GW_HWNDNEXT))
            ProcedureReturn 1
        EndSelect
        
    EndSelect
    ProcedureReturn CallGadgetProc(hWnd,uMsg,wParam,lParam)
  EndProcedure
  
  If OpenWindow(0,0,0,740,250,"Example SetGadgetCallback",#PB_Window_SystemMenu|#PB_Window_ScreenCentered)
    EditorGadget(1,10,10,230,200)
    EditorGadget(2,250,10,230,200)
    EditorGadget(3,490,10,230,200)
    
    SetGadgetCallback(1, @RichEditProc())
    SetGadgetCallback(2, @RichEditProc())
    SetGadgetCallback(3, @RichEditProc())
    
    ; SetGadgetCallback(1, 0)
    ; SetGadgetCallback(2, 0)
    
    Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
  EndIf
  
CompilerEndIf
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
wombats
Enthusiast
Enthusiast
Posts: 663
Joined: Thu Dec 29, 2011 5:03 pm

Re: disable backspace/del?

Post by wombats »

I doubt this is the best way to do it, but it seems to work. I tested it on Windows and macOS.

Code: Select all

Procedure ScintillaCallBack(Gadget, *scinotify.SCNotification)
  Protected currentLine, position
  Select *scinotify\nmhdr\code
    Case #SCN_UPDATEUI
      position = ScintillaSendMessage(0, #SCI_GETCURRENTPOS)
      currentLine = ScintillaSendMessage(0, #SCI_LINEFROMPOSITION, position)
      If currentLine = ScintillaSendMessage(0, #SCI_GETLINECOUNT) - 1
        AddKeyboardShortcut(0, #PB_Shortcut_Back, 0)
        AddKeyboardShortcut(0, #PB_Shortcut_Delete, 0)
      Else
        RemoveKeyboardShortcut(0, #PB_Shortcut_Back)
        RemoveKeyboardShortcut(0, #PB_Shortcut_Delete)
      EndIf
    EndSelect
EndProcedure

If OpenWindow(0, 0, 0, 330, 90, "ScintillaGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  If InitScintilla()
    ScintillaGadget(0, 10, 10, 320, 70, @ScintillaCallBack())
    Define txt$ = "First line" + Chr(10) + "Second line" + Chr(10) + "Third line (no delete/backspace)"
    ScintillaSendMessage(0, #SCI_SETTEXT, 0, UTF8(txt$))
  EndIf
  Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
EndIf
breeze4me
Enthusiast
Enthusiast
Posts: 511
Joined: Thu Mar 09, 2006 9:24 am
Location: S. Kor

Re: disable backspace/del?

Post by breeze4me »

Code: Select all

Procedure ScintillaCallBack(Gadget, *scinotify.SCNotification)
  
  If *scinotify\nmhdr\code = #SCN_UPDATEUI
    If ScintillaSendMessage(Gadget, #SCI_LINEFROMPOSITION, ScintillaSendMessage(Gadget, #SCI_GETCURRENTPOS)) = ScintillaSendMessage(Gadget, #SCI_GETLINECOUNT) - 1
      ScintillaSendMessage(Gadget, #SCI_CLEARCMDKEY, #SCK_BACK)
      ScintillaSendMessage(Gadget, #SCI_CLEARCMDKEY, #SCK_DELETE)
    Else
      ScintillaSendMessage(Gadget, #SCI_ASSIGNCMDKEY, #SCK_BACK, #SCI_DELETEBACK)
      ScintillaSendMessage(Gadget, #SCI_ASSIGNCMDKEY, #SCK_DELETE, #SCI_CLEAR)
    EndIf
    
  EndIf
  
EndProcedure

If OpenWindow(0, 0, 0, 330, 90, "ScintillaGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  If InitScintilla()
    ScintillaGadget(0, 10, 10, 320, 70, @ScintillaCallBack())
    Define txt$ = "First line" + Chr(10) + "Second line" + Chr(10) + "Third line (no delete/backspace)"
    ScintillaSendMessage(0, #SCI_SETTEXT, 0, UTF8(txt$))
  EndIf
  Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
EndIf

Code: Select all

Procedure ScintillaCallBack(Gadget, *scinotify.SCNotification)
  
  If *scinotify\nmhdr\code = #SCN_UPDATEUI
    If ScintillaSendMessage(Gadget, #SCI_LINEFROMPOSITION, ScintillaSendMessage(Gadget, #SCI_GETCURRENTPOS)) = ScintillaSendMessage(Gadget, #SCI_GETLINECOUNT) - 1
      ScintillaSendMessage(Gadget, #SCI_ASSIGNCMDKEY, #SCK_BACK, #SCI_DELETEBACK)
      ScintillaSendMessage(Gadget, #SCI_ASSIGNCMDKEY, #SCK_DELETE, #SCI_CLEAR)
    Else
      ScintillaSendMessage(Gadget, #SCI_CLEARCMDKEY, #SCK_BACK)
      ScintillaSendMessage(Gadget, #SCI_CLEARCMDKEY, #SCK_DELETE)
    EndIf
    
  EndIf
  
EndProcedure

If OpenWindow(0, 0, 0, 330, 90, "ScintillaGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  If InitScintilla()
    ScintillaGadget(0, 10, 10, 320, 70, @ScintillaCallBack())
    Define txt$ = "First line (no delete/backspace)" + Chr(10) + "Second line (no delete/backspace)" + Chr(10) + "Third line"
    ScintillaSendMessage(0, #SCI_SETTEXT, 0, UTF8(txt$))
  EndIf
  Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
EndIf
User avatar
kenmo
Addict
Addict
Posts: 1967
Joined: Tue Dec 23, 2003 3:54 am

Re: disable backspace/del?

Post by kenmo »

1. Spam account, this is an exact copy of an older question:
viewtopic.php?f=13&t=66077

2. Good solutions anyway :)

3. Backspace and Del are disabled, but beware you can still Ctrl+X to cut text, highlight-and-type to overwrite text, etc... it's complicated

4. What an odd request :?: A text field in which Backspace is disabled depending on the caret line. But is adding text allowed? If delete/add are both disallowed, you could set the READONLY flag depending on current caret position. Hm.
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4636
Joined: Sun Apr 12, 2009 6:27 am

Re: disable backspace/del?

Post by RASHAD »

While it is a spam :)
But taking kenmo comments into account

Code: Select all

Global Flag

Procedure ScintillaCallBack(Gadget, *scinotify.SCNotification)
 
  If *scinotify\nmhdr\code = #SCN_UPDATEUI
    If ScintillaSendMessage(Gadget, #SCI_LINEFROMPOSITION, ScintillaSendMessage(Gadget, #SCI_GETCURRENTPOS)) = ScintillaSendMessage(Gadget, #SCI_GETLINECOUNT) - 1
      ScintillaSendMessage(Gadget, #SCI_ASSIGNCMDKEY, #SCK_BACK, #SCI_DELETEBACK)
      ScintillaSendMessage(Gadget, #SCI_ASSIGNCMDKEY, #SCK_DELETE, #SCI_CLEAR)
      Flag = 1
    Else
      ScintillaSendMessage(Gadget, #SCI_CLEARCMDKEY, #SCK_BACK)
      ScintillaSendMessage(Gadget, #SCI_CLEARCMDKEY, #SCK_DELETE)
      Flag = 0
    EndIf
   
  EndIf
 
EndProcedure

If OpenWindow(0, 0, 0, 350, 150, "ScintillaGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  If InitScintilla()
    ScintillaGadget(0, 10, 10, 330, 130, @ScintillaCallBack())
    Define txt$ = "First line (no delete/backspace)" + Chr(10) + "Second line (no delete/backspace)" + Chr(10) + "Third line"
    ScintillaSendMessage(0, #SCI_SETTEXT, 0, UTF8(txt$))
  EndIf

Repeat
  Select WaitWindowEvent(10)
    Case #PB_Event_CloseWindow
      Quit = 1
      
    Case #WM_KEYUP
      If GetActiveGadget() = 0 And Flag = 0 And EventwParam() = 16
        pos = ScintillaSendMessage(0,#SCI_GETCURRENTPOS)
        ScintillaSendMessage(0, #SCI_SETSEL , pos,pos)
      EndIf
         
    Case #WM_LBUTTONUP
      If GetActiveGadget() = 0 And Flag = 0
        pos = ScintillaSendMessage(0,#SCI_GETCURRENTPOS)
        ScintillaSendMessage(0, #SCI_SETSEL , pos,pos)
      EndIf
     
  EndSelect 
Until Quit = 1
EndIf
Egypt my love
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4636
Joined: Sun Apr 12, 2009 6:27 am

Re: disable backspace/del?

Post by RASHAD »

Taking Shift,Ctrl & Alt into account
Caret position at the proper position

Code: Select all

Global Flag

Procedure ScintillaCallBack(Gadget, *scinotify.SCNotification)
 
  If *scinotify\nmhdr\code = #SCN_UPDATEUI
    If ScintillaSendMessage(Gadget, #SCI_LINEFROMPOSITION, ScintillaSendMessage(Gadget, #SCI_GETCURRENTPOS)) = ScintillaSendMessage(Gadget, #SCI_GETLINECOUNT) - 1
      ScintillaSendMessage(Gadget, #SCI_ASSIGNCMDKEY, #SCK_BACK, #SCI_DELETEBACK)
      ScintillaSendMessage(Gadget, #SCI_ASSIGNCMDKEY, #SCK_BACK + (#SCMOD_SHIFT << 16),#SCI_DELETEBACK)
      ScintillaSendMessage(Gadget, #SCI_ASSIGNCMDKEY, #SCK_BACK + (#SCMOD_CTRL << 16),#SCI_DELETEBACK)
      ScintillaSendMessage(Gadget, #SCI_ASSIGNCMDKEY, #SCK_BACK + (#SCMOD_ALT << 16),#SCI_DELETEBACK)
      ScintillaSendMessage(Gadget, #SCI_ASSIGNCMDKEY, #SCK_DELETE, #SCI_CLEAR)
      ScintillaSendMessage(Gadget, #SCI_ASSIGNCMDKEY, #SCK_DELETE + (#SCMOD_SHIFT << 16),#SCI_CLEAR)
      ScintillaSendMessage(Gadget, #SCI_ASSIGNCMDKEY, #SCK_DELETE + (#SCMOD_CTRL << 16),#SCI_CLEAR)
      ScintillaSendMessage(Gadget, #SCI_ASSIGNCMDKEY, #SCK_DELETE + (#SCMOD_ALT << 16),#SCI_CLEAR)
      Flag = 1
    Else
      ScintillaSendMessage(Gadget, #SCI_CLEARCMDKEY, #SCK_BACK)
      ScintillaSendMessage(Gadget, #SCI_CLEARCMDKEY, #SCK_BACK + (#SCMOD_SHIFT << 16))
      ScintillaSendMessage(Gadget, #SCI_CLEARCMDKEY, #SCK_BACK + (#SCMOD_CTRL << 16))
      ScintillaSendMessage(Gadget, #SCI_CLEARCMDKEY, #SCK_BACK + (#SCMOD_ALT << 16))
      ScintillaSendMessage(Gadget, #SCI_CLEARCMDKEY, #SCK_DELETE)
      ScintillaSendMessage(Gadget, #SCI_CLEARCMDKEY, #SCK_DELETE + (#SCMOD_SHIFT << 16))
      ScintillaSendMessage(Gadget, #SCI_CLEARCMDKEY, #SCK_DELETE + (#SCMOD_CTRL << 16))
      ScintillaSendMessage(Gadget, #SCI_CLEARCMDKEY, #SCK_DELETE + (#SCMOD_ALT << 16))
      Flag = 0
    EndIf
   
  EndIf
 
EndProcedure

If OpenWindow(0, 0, 0, 350, 150, "ScintillaGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  If InitScintilla()
    ScintillaGadget(0, 10, 10, 330, 130, @ScintillaCallBack())
    Define txt$ = "First line (no delete/backspace)" + Chr(10) + "Second line (no delete/backspace)" + Chr(10) + "Third line"
    ScintillaSendMessage(0, #SCI_SETTEXT, 0, UTF8(txt$))
  EndIf

Repeat
  Select WaitWindowEvent(10)
    Case #PB_Event_CloseWindow
      Quit = 1
      
    Case #WM_KEYUP
      If GetActiveGadget() = 0 And Flag = 0 And EventwParam() = 16      
        startpos = ScintillaSendMessage(0,#SCI_GETCURRENTPOS) - ScintillaSendMessage(0, #SCI_GETSELTEXT,0,0)+1
        endpos = ScintillaSendMessage(0,#SCI_GETCURRENTPOS)
        ScintillaSendMessage(0, #SCI_SETSEL , startpos,startpos)
      EndIf
         
    Case #WM_LBUTTONUP
      If GetActiveGadget() = 0 And Flag = 0
        startpos = ScintillaSendMessage(0,#SCI_GETCURRENTPOS) - ScintillaSendMessage(0, #SCI_GETSELTEXT,0,0)+1
        ScintillaSendMessage(0, #SCI_SETSEL , startpos,startpos)
      EndIf
     
  EndSelect 
Until Quit = 1
EndIf
Egypt my love
User avatar
Michael Vogel
Addict
Addict
Posts: 2666
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

Re: disable backspace/del?

Post by Michael Vogel »

Not sure what's exactly the target here (the title of the first posting is "Re: disable backspace/del?"), but wouldn't it be enough to use the internal KeyboardShortcut function?

Code: Select all


Procedure DisableKeys(mode)

	If mode
		AddKeyboardShortcut(0,#PB_Shortcut_Back,9991)
		AddKeyboardShortcut(0,#PB_Shortcut_Delete,9992)
	Else
		RemoveKeyboardShortcut(0,#PB_Shortcut_Back)
		RemoveKeyboardShortcut(0,#PB_Shortcut_Delete)
	EndIf

EndProcedure

If OpenWindow(0,0,0,740,250,"Example SetGadgetCallback",#PB_Window_SystemMenu|#PB_Window_ScreenCentered)
	EditorGadget(1,10,10,230,200)
	EditorGadget(2,250,10,230,200)
	EditorGadget(3,490,10,230,200)

	SetGadgetText(1,"Hey Basic!")
	SetActiveGadget(1)

	Repeat
		Select WaitWindowEvent()
		Case #PB_Event_CloseWindow
			End
		Case #PB_Event_Gadget,#PB_Event_Menu
			g=EventGadget()
			
			If IsGadget(g)
				Select GadgetType(g)
				Case #PB_GadgetType_Editor,#PB_GadgetType_String
					Select EventType()
					Case #PB_EventType_Focus
						DisableKeys(#True)
					Case #PB_EventType_LostFocus
						DisableKeys(#False)
					EndSelect
				EndSelect

			Else
				Debug StringField("Back.Delete",g-9990,".")

			EndIf

		EndSelect
	ForEver

EndIf
wombats
Enthusiast
Enthusiast
Posts: 663
Joined: Thu Dec 29, 2011 5:03 pm

Re: disable backspace/del?

Post by wombats »

Michael Vogel wrote:Not sure what's exactly the target here (the title of the first posting is "Re: disable backspace/del?"), but wouldn't it be enough to use the internal KeyboardShortcut function?

Code: Select all


Procedure DisableKeys(mode)

	If mode
		AddKeyboardShortcut(0,#PB_Shortcut_Back,9991)
		AddKeyboardShortcut(0,#PB_Shortcut_Delete,9992)
	Else
		RemoveKeyboardShortcut(0,#PB_Shortcut_Back)
		RemoveKeyboardShortcut(0,#PB_Shortcut_Delete)
	EndIf

EndProcedure

If OpenWindow(0,0,0,740,250,"Example SetGadgetCallback",#PB_Window_SystemMenu|#PB_Window_ScreenCentered)
	EditorGadget(1,10,10,230,200)
	EditorGadget(2,250,10,230,200)
	EditorGadget(3,490,10,230,200)

	SetGadgetText(1,"Hey Basic!")
	SetActiveGadget(1)

	Repeat
		Select WaitWindowEvent()
		Case #PB_Event_CloseWindow
			End
		Case #PB_Event_Gadget,#PB_Event_Menu
			g=EventGadget()
			
			If IsGadget(g)
				Select GadgetType(g)
				Case #PB_GadgetType_Editor,#PB_GadgetType_String
					Select EventType()
					Case #PB_EventType_Focus
						DisableKeys(#True)
					Case #PB_EventType_LostFocus
						DisableKeys(#False)
					EndSelect
				EndSelect

			Else
				Debug StringField("Back.Delete",g-9990,".")

			EndIf

		EndSelect
	ForEver

EndIf
It seems the original post was deleted.
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4636
Joined: Sun Apr 12, 2009 6:27 am

Re: disable backspace/del?

Post by RASHAD »

@Michael
The original post was about Scintilla gadget (It was a spam so the moderator delete it)
And taking care about kenmo comments
Egypt my love
Post Reply