Accueil > > > EN REMPLACEMENT DE SENDKEYS
EN REMPLACEMENT DE SENDKEYS
Information sur la source
Description
Généralement, SendKeys n'est pas recommandé dans un environnement de production, car la séquence de clé est interprétée par la fenêtre actuellement active. De toutes évidence, cela peut créer des comportements imprévisible, à tout le moins qu'on peut dire, dans le cas où une autre application recevrait le focus lorsque votre code initie le SendKeys. Un cas extrême est d'envoyer un "Y" à une application qui attendait une confirmation pour reformater votre disque principal. Donc, en définitive, éviter d'utiliser SendKey.
Source
- ' Déclare un Type en prévision d'un appel d'API futur
- Private Type OSVERSIONINFO
- dwOSVersionInfoSize As Long
- dwMajorVersion As Long
- dwMinorVersion As Long
- dwBuildNumber As Long
- dwPlatformId As Long
- szCSDVersion As String * 128 ' Maintenance pour utilisation de PSS
- End Type
-
- ' API, déclarations:
- Private Declare Function GetVersionEx Lib "Kernel32" _
- Alias "GetVersionExA" _
- (lpVersionInformation As OSVERSIONINFO) As Long
-
- Private Declare Sub keybd_event Lib "user32" _
- (ByVal bVk As Byte, _
- ByVal bScan As Byte, _
- ByVal dwflags As Long, ByVal dwExtraInfo As Long)
-
- Private Declare Function GetKeyboardState Lib "user32" _
- (pbKeyState As Byte) As Long
-
- Private Declare Function SetKeyboardState Lib "user32" _
- (lppbKeyState As Byte) As Long
-
- ' Constants, déclarations:
- Const VK_NUMLOCK = &H90
- Const VK_SCROLL = &H91
- Const VK_CAPITAL = &H14
- Const KEYEVENTF_EXTENDEDKEY = &H1
- Const KEYEVENTF_KEYUP = &H2
- Const VER_PLATFORM_WIN32_NT = 2
- Const VER_PLATFORM_WIN32_WINDOWS = 1
-
- Function IsCapsLockOn() As Boolean
- Dim o As OSVERSIONINFO
-
- o.dwOSVersionInfoSize = Len(o)
- GetVersionEx o
- Dim keys(0 To 255) As Byte
- GetKeyboardState keys(0)
- IsCapsLockOn = keys(VK_CAPITAL)
- End Function
-
- Sub ToggleCapsLock()
- Dim o As OSVERSIONINFO
-
- o.dwOSVersionInfoSize = Len(o)
- GetVersionEx o
- Dim keys(0 To 255) As Byte
- GetKeyboardState keys(0)
-
- If o.dwPlatformId = VER_PLATFORM_WIN32_WINDOWS Then '=====Win95
- 'Bascule capslock
- keys(VK_CAPITAL) = Abs(Not keys(VK_CAPITAL))
- SetKeyboardState keys(0)
- ElseIf o.dwPlatformId = VER_PLATFORM_WIN32_NT Then '=====WinNT
- 'Simule Key Press>
- keybd_event VK_CAPITAL, &H45, KEYEVENTF_EXTENDEDKEY Or 0, 0
- 'Simule Key Release
- keybd_event VK_CAPITAL, &H45, KEYEVENTF_EXTENDEDKEY _
- Or KEYEVENTF_KEYUP, 0
- End If
- End Sub
-
- Function IsNumLockOn() As Boolean
- Dim o As OSVERSIONINFO
-
- o.dwOSVersionInfoSize = Len(o)
- GetVersionEx o
- Dim keys(0 To 255) As Byte
- GetKeyboardState keys(0)
- IsNumLockOn = keys(VK_NUMLOCK)
- End Function
-
- Sub ToggleNumLock()
- Dim o As OSVERSIONINFO
-
- o.dwOSVersionInfoSize = Len(o)
- GetVersionEx o
- Dim keys(0 To 255) As Byte
- GetKeyboardState keys(0)
-
- If o.dwPlatformId = VER_PLATFORM_WIN32_WINDOWS Then '=====Win95
- keys(VK_NUMLOCK) = Abs(Not keys(VK_NUMLOCK))
- SetKeyboardState keys(0)
- ElseIf o.dwPlatformId = VER_PLATFORM_WIN32_NT Then '=====WinNT
- 'Simule Key Press
- keybd_event VK_NUMLOCK, &H45, KEYEVENTF_EXTENDEDKEY Or 0, 0
- 'Simule Key Release
- keybd_event VK_NUMLOCK, &H45, KEYEVENTF_EXTENDEDKEY _
- Or KEYEVENTF_KEYUP, 0
- End If
-
- End Sub
-
- Function IsScrollLockOn()
- Dim o As OSVERSIONINFO
-
- o.dwOSVersionInfoSize = Len(o)
- GetVersionEx o
- Dim keys(0 To 255) As Byte
- GetKeyboardState keys(0)
- IsScrollLockOn = keys(VK_SCROLL)
- End Function
-
- Sub ToggleScrollLock()
- Dim o As OSVERSIONINFO
-
- o.dwOSVersionInfoSize = Len(o)
- GetVersionEx o
- Dim keys(0 To 255) As Byte
- GetKeyboardState keys(0)
- If o.dwPlatformId = VER_PLATFORM_WIN32_WINDOWS Then '=====Win95
- keys(VK_SCROLL) = Abs(Not keys(VK_SCROLL))
- SetKeyboardState keys(0)
- ElseIf o.dwPlatformId = VER_PLATFORM_WIN32_NT Then '=====WinNT
- 'Simule Key Press
- keybd_event VK_SCROLL, &H45, KEYEVENTF_EXTENDEDKEY Or 0, 0
- 'Simule Key Release
- keybd_event VK_SCROLL, &H45, KEYEVENTF_EXTENDEDKEY Or KEYEVENTF_KEYUP, 0
- End If
- End Sub
-
- Sub mySendKeys(sKeys As String, Optional bWait As Boolean = False)
- Dim bNumLockState As Boolean
- Dim bCapsLockState As Boolean
- Dim bScrollLockState As Boolean
- bNumLockState = IsNumLockOn()
- bCapsLockState = IsCapsLockOn()
- bScrollLockState = IsScrollLockOn()
- SendKeys sKeys, bWait
- If IsNumLockOn() <> bNumLockState Then
- ToggleNumLock
- End If
- If IsCapsLockOn() <> bCapsLockState Then
- ToggleCapsLock
- End If
- If IsScrollLockOn() <> bScrollLockState Then
- ToggleScrollLock
- End If
- End Sub
-
- Function fSendKeys(sKeys As String, Optional bWait As Boolean = False)
- ' En faire une fonction, qu'on puisse l'appeler à partir de macro
- mySendKeys sKeys, bWait
- End Function
-
' Déclare un Type en prévision d'un appel d'API futur
Private Type OSVERSIONINFO
dwOSVersionInfoSize As Long
dwMajorVersion As Long
dwMinorVersion As Long
dwBuildNumber As Long
dwPlatformId As Long
szCSDVersion As String * 128 ' Maintenance pour utilisation de PSS
End Type
' API, déclarations:
Private Declare Function GetVersionEx Lib "Kernel32" _
Alias "GetVersionExA" _
(lpVersionInformation As OSVERSIONINFO) As Long
Private Declare Sub keybd_event Lib "user32" _
(ByVal bVk As Byte, _
ByVal bScan As Byte, _
ByVal dwflags As Long, ByVal dwExtraInfo As Long)
Private Declare Function GetKeyboardState Lib "user32" _
(pbKeyState As Byte) As Long
Private Declare Function SetKeyboardState Lib "user32" _
(lppbKeyState As Byte) As Long
' Constants, déclarations:
Const VK_NUMLOCK = &H90
Const VK_SCROLL = &H91
Const VK_CAPITAL = &H14
Const KEYEVENTF_EXTENDEDKEY = &H1
Const KEYEVENTF_KEYUP = &H2
Const VER_PLATFORM_WIN32_NT = 2
Const VER_PLATFORM_WIN32_WINDOWS = 1
Function IsCapsLockOn() As Boolean
Dim o As OSVERSIONINFO
o.dwOSVersionInfoSize = Len(o)
GetVersionEx o
Dim keys(0 To 255) As Byte
GetKeyboardState keys(0)
IsCapsLockOn = keys(VK_CAPITAL)
End Function
Sub ToggleCapsLock()
Dim o As OSVERSIONINFO
o.dwOSVersionInfoSize = Len(o)
GetVersionEx o
Dim keys(0 To 255) As Byte
GetKeyboardState keys(0)
If o.dwPlatformId = VER_PLATFORM_WIN32_WINDOWS Then '=====Win95
'Bascule capslock
keys(VK_CAPITAL) = Abs(Not keys(VK_CAPITAL))
SetKeyboardState keys(0)
ElseIf o.dwPlatformId = VER_PLATFORM_WIN32_NT Then '=====WinNT
'Simule Key Press>
keybd_event VK_CAPITAL, &H45, KEYEVENTF_EXTENDEDKEY Or 0, 0
'Simule Key Release
keybd_event VK_CAPITAL, &H45, KEYEVENTF_EXTENDEDKEY _
Or KEYEVENTF_KEYUP, 0
End If
End Sub
Function IsNumLockOn() As Boolean
Dim o As OSVERSIONINFO
o.dwOSVersionInfoSize = Len(o)
GetVersionEx o
Dim keys(0 To 255) As Byte
GetKeyboardState keys(0)
IsNumLockOn = keys(VK_NUMLOCK)
End Function
Sub ToggleNumLock()
Dim o As OSVERSIONINFO
o.dwOSVersionInfoSize = Len(o)
GetVersionEx o
Dim keys(0 To 255) As Byte
GetKeyboardState keys(0)
If o.dwPlatformId = VER_PLATFORM_WIN32_WINDOWS Then '=====Win95
keys(VK_NUMLOCK) = Abs(Not keys(VK_NUMLOCK))
SetKeyboardState keys(0)
ElseIf o.dwPlatformId = VER_PLATFORM_WIN32_NT Then '=====WinNT
'Simule Key Press
keybd_event VK_NUMLOCK, &H45, KEYEVENTF_EXTENDEDKEY Or 0, 0
'Simule Key Release
keybd_event VK_NUMLOCK, &H45, KEYEVENTF_EXTENDEDKEY _
Or KEYEVENTF_KEYUP, 0
End If
End Sub
Function IsScrollLockOn()
Dim o As OSVERSIONINFO
o.dwOSVersionInfoSize = Len(o)
GetVersionEx o
Dim keys(0 To 255) As Byte
GetKeyboardState keys(0)
IsScrollLockOn = keys(VK_SCROLL)
End Function
Sub ToggleScrollLock()
Dim o As OSVERSIONINFO
o.dwOSVersionInfoSize = Len(o)
GetVersionEx o
Dim keys(0 To 255) As Byte
GetKeyboardState keys(0)
If o.dwPlatformId = VER_PLATFORM_WIN32_WINDOWS Then '=====Win95
keys(VK_SCROLL) = Abs(Not keys(VK_SCROLL))
SetKeyboardState keys(0)
ElseIf o.dwPlatformId = VER_PLATFORM_WIN32_NT Then '=====WinNT
'Simule Key Press
keybd_event VK_SCROLL, &H45, KEYEVENTF_EXTENDEDKEY Or 0, 0
'Simule Key Release
keybd_event VK_SCROLL, &H45, KEYEVENTF_EXTENDEDKEY Or KEYEVENTF_KEYUP, 0
End If
End Sub
Sub mySendKeys(sKeys As String, Optional bWait As Boolean = False)
Dim bNumLockState As Boolean
Dim bCapsLockState As Boolean
Dim bScrollLockState As Boolean
bNumLockState = IsNumLockOn()
bCapsLockState = IsCapsLockOn()
bScrollLockState = IsScrollLockOn()
SendKeys sKeys, bWait
If IsNumLockOn() <> bNumLockState Then
ToggleNumLock
End If
If IsCapsLockOn() <> bCapsLockState Then
ToggleCapsLock
End If
If IsScrollLockOn() <> bScrollLockState Then
ToggleScrollLock
End If
End Sub
Function fSendKeys(sKeys As String, Optional bWait As Boolean = False)
' En faire une fonction, qu'on puisse l'appeler à partir de macro
mySendKeys sKeys, bWait
End Function
Conclusion
Auteur : Dev Ashish
Sources du même auteur
Sources de la même categorie
Commentaires et avis
|
Derniers Blogs
TECHDAYS PARIS 2012 : WINDOWS SERVER "8" QUOI DE 9 !TECHDAYS PARIS 2012 : WINDOWS SERVER "8" QUOI DE 9 ! par ROMELARD Fabrice
Speakers: Fabrice Meillon et Stanislas Quastana Cette session est basée entièrement sur celle donnée lors de la BUILD cet hiver. Il n'y a pas d'ajout d'information en rapport avec cet évènement passé. Windows 8 Server sera intégralem...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice [HTML5] AUTOUR DU W3C : NOUVEAUX STANDARDS ET WEB MOBILE (LILLE)[HTML5] AUTOUR DU W3C : NOUVEAUX STANDARDS ET WEB MOBILE (LILLE) par Gio
Je m'y prends un peu tard je sais, mais bon je suis développeur web et donc hyper fainéant ! Toujours dans le cadre des technologies émergentes, ici HTML5, parce qu'on aime HTML5 chez Wyg , nous seront présent, le vieux ( Aurélien V.) et moi, pour pr...
Cliquez pour lire la suite de l'article par Gio [WP7] DYNAMICALLY CHANGE STARTUP PAGE[WP7] DYNAMICALLY CHANGE STARTUP PAGE par KooKiz
Let's say that you want to allow the user to customize the startup page of your application. You can easily change the startup page by editing the 'NavigationPage' attribute in the manifest file. But the manifest cannot be modified once the applicatio...
Cliquez pour lire la suite de l'article par KooKiz SESSION SILVERLIGHT 5 3D : SLIDES ET DEMOSSESSION SILVERLIGHT 5 3D : SLIDES ET DEMOS par Groc
Durant les techdays, j'ai eu le plaisir d'animer une session sur Silverlight 5 et la 3D avec Simon Ferquel. Comme promis, voici nos slides et mes démos (celles avec le viper BSG) ici et là. Pour mémoire, les démos utilisent toutes le viper BSG...
Cliquez pour lire la suite de l'article par Groc
Logiciels
DocTranslate (V3.1.0.0)DOCTRANSLATE (V3.1.0.0)DocTranslate est un traducteur de document Microsoft Word, PowerPoint et Excel. Il permet d'autom... Cliquez pour télécharger DocTranslate Tribler (2012)TRIBLER (2012)Tribler est un client pair à pair (P2P/Peer-to-Peer) open source avec la capacité de regarder des... Cliquez pour télécharger Tribler OneSwarm (2012)ONESWARM (2012)Le peer-to-peer qui protège votre vie privée, c'est OneSwarm.
Ce logiciel de peer-to-peer crypté... Cliquez pour télécharger OneSwarm PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO (V8.4)PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO (V8.4)PONAMEDIA TV DEVIENS HELLLOOO FLASH
LA TV SUR VOTRE ORDINATEUR.
Toute une plateforme Multi... Cliquez pour télécharger PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO Academy System (17.2.1.0)ACADEMY SYSTEM (17.2.1.0)Logiciel de gestion des établissements.
- élèves/étudiants (inscription, dossier, absence...)
-... Cliquez pour télécharger Academy System
|