#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#region ### START Koda GUI section ### Form=
Global $bSel = False
$Form1 = GUICreate("Form1", 316, 220)
$Group1 = GUICtrlCreateGroup("Group1", 8, 8, 297, 200)
$Input1 = GUICtrlCreateInput("123456789abcdefg", 48, 48, 233, 21)
GUICtrlCreateInput("完全模拟按键:有输入焦点时,操作才有效", 48, 95, 233, 21)
$BacKspace = GUICtrlCreateButton("发送退格", 84, 150, 60, 25)
$BacKspace2 = GUICtrlCreateButton("模拟退格", 170, 150, 60, 25)
GUICtrlSetState(-1, $gui_focus)
GUICtrlCreateGroup("", -99, -99, 1, 1)
GUISetState(@SW_SHOW)
#endregion ### END Koda GUI section ###
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
Exit
Case $GUI_EVENT_PRIMARYUP, $GUI_EVENT_SECONDARYUP
_Sel_not()
Case $BacKspace
_BackSpace()
Case $BacKspace2
_BackSpace_2()
EndSwitch
WEnd
Func _Sel_not()
Local $aCur = GUIGetCursorInfo($Form1)
If IsArray($aCur) Then
If $aCur[4] == $BacKspace Then Return
If $aCur[4] == $BacKspace2 Then Return
If $aCur[4] == $Input1 Then
$bSel = True
Else
$bSel = False
EndIf
EndIf
EndFunc ;==>MY_WM_COMMAND
Func _BackSpace_2()
If Not $bSel Then Return
; 有输入焦点时,才处理
Local $str = GUICtrlRead($Input1)
Local $iLen, $iCol
Local $sLeft, $sRight, $sSel
$sSel = ControlCommand($Form1, '', $Input1, 'GetSelected') ;选中的字符
$iCol = ControlCommand($Form1, '', $Input1, 'GetCurrentCol');插入点位置
If $sSel == 0 Then ;未选取字符
If $iCol > 1 Then ;输入焦点在第1个字符后面,只删除插入点前一字符.
$sLeft = StringMid($str, 1, $iCol - 2)
$sRight = StringMid($str, $iCol)
GUICtrlSetData($Input1, $sLeft & $sRight)
GUICtrlSendMsg($Input1, $EM_SETSEL, $iCol - 2, $iCol - 2)
EndIf
Else
$iLen = StringLen($sSel)
$sLeft = StringMid($str, 1, $iCol - 1)
$sRight = StringMid($str, $iCol + $iLen)
GUICtrlSetData($Input1, $sLeft & $sRight)
GUICtrlSendMsg($Input1, $EM_SETSEL, $iCol - 1, $iCol - 1)
EndIf
ControlFocus($Form1, '', $Input1)
EndFunc ;==>_BackSpace
Func _BackSpace()
If Not $bSel Then Return
; 有输入焦点时,才处理
ControlFocus($Form1, '', $Input1)
Send('{BACKSPACE}')
EndFunc