找回密码
 加入
搜索
查看: 1880|回复: 5

[AU3基础] 怎么把键盘上的BacKspace退格键功能写到控件Button上(已解决)

[复制链接]
发表于 2018-2-24 16:14:23 | 显示全部楼层 |阅读模式
本帖最后由 qsy666888 于 2018-3-6 14:52 编辑

怎么把键盘上的BacKspace退格键功能写到控件Button上, 我试了几次没有弄得成功
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 316, 251)
$Group1 = GUICtrlCreateGroup("Group1", 8, 8, 297, 233)
$Input1 = GUICtrlCreateInput("123456789abcdefg", 48, 48, 233, 21)
$BacKspace = GUICtrlCreateButton("退格(BacKspace)", 104, 136, 130, 25)
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  $BacKspace
                        
Sleep(100)
MouseUp("left")
        EndSwitch
WEnd
发表于 2018-2-24 19:49:41 | 显示全部楼层
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 316, 251)
$Group1 = GUICtrlCreateGroup("Group1", 8, 8, 297, 233)
$Input1 = GUICtrlCreateInput("123456789abcdefg", 48, 48, 233, 21)
$BacKspace = GUICtrlCreateButton("退格(BacKspace)", 104, 136, 130, 25)
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 $BacKspace
                        ControlFocus($Form1, "", $Input1)
                        ;从后删除这个好
                        ; GUICtrlSetState($Input1, $GUI_FOCUS)
                        ;Send("{END}")
                        Send("{BACKSPACE}")
                        Sleep(100)
                        MouseUp("left")
        EndSwitch
WEnd
 楼主| 发表于 2018-2-24 21:27:05 | 显示全部楼层
回复 2# 志艺风云
谢谢谢谢
发表于 2018-2-24 22:05:23 | 显示全部楼层
2楼的代码是个"伪退格"功能.

我这个才是真正的退格,可以直接发送退格键,也可以不发送任何按键.
#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
发表于 2018-2-24 22:05:34 | 显示全部楼层
本帖最后由 Alam 于 2018-2-24 22:22 编辑

简化一下,上楼的关键函数.
Func _BackSpace_2()
        If Not $bSel Then Return
        ; 有输入焦点时,才处理

        Local $str = GUICtrlRead($Input1)
        Local $aR, $sLeft, $sRight

        $aR = GUICtrlRecvMsg($Input1, $EM_GETSEL)
        If Not IsArray($aR) Then Return
        If $aR[1] == 1 Then Return
        $sLeft = StringMid($str, 1, $aR[0] - 1)
        $sRight = StringMid($str, $aR[1] + 1)
        GUICtrlSetData($Input1, $sLeft & $sRight)
        GUICtrlSendMsg($Input1, $EM_SETSEL, $aR[0], $aR[0])

        ControlFocus($Form1, '', $Input1)
EndFunc   ;==>_BackSpace_2
 楼主| 发表于 2018-2-24 22:40:53 | 显示全部楼层
回复 5# Alam
谢谢谢谢,高手在民间哈
不过遇到一个问题,
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 260, 188, 192, 124)
$Input1 = GUICtrlCreateInput("", 40, 24, 185, 40, $ES_MULTILINE);高度不变的情况下,现在有两行的限制,能否继续向下输入
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
        $nMsg = GUIGetMsg()
        Switch $nMsg
                Case $GUI_EVENT_CLOSE
                        Exit

        EndSwitch
WEnd
您需要登录后才可以回帖 登录 | 加入

本版积分规则

QQ|手机版|小黑屋|AUTOIT CN ( 鲁ICP备19019924号-1 )谷歌 百度

GMT+8, 2024-9-28 09:22 , Processed in 0.080130 second(s), 19 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表