Je cherche à enclencher "Num Lock" par VBA à l'ouverture d'une feuille Excel
quel que soit sont état précédent.
J'en suis au test suivant:
=====================================
Private Declare Function GetKeyboardState Lib "user32" (pbKeyState As Byte)
As Long
Sub NumState()
Const VK_NUMLOCK = &H90
Dim KBState(0 To 255) As Byte
GetKeyboardState KBState(0)
MsgBox KBState(VK_NUMLOCK) '1 -> ON, 0 -> OFF
End Sub
=====================================
Mais je ne sais pas comment lui retourner "1" si la réponse au test est "0"
pour endifier l'état.. :-(
Cette action est irreversible, confirmez la suppression du commentaire ?
Signaler le commentaire
Veuillez sélectionner un problème
Nudité
Violence
Harcèlement
Fraude
Vente illégale
Discours haineux
Terrorisme
Autre
Michel Pierron
Bonjour Emile; Le fonctionnement de Num lock et Caps lock dépend de la version de ton Operating System: Option Explicit Type OSVERSIONINFO dwOSVersionInfoSize As Long dwMajorVersion As Long dwMinorVersion As Long dwBuildNumber As Long dwPlatformId As Long szCSDVersion As String * 128 End Type 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
' Sets NUMLOCK and CAPSLOCK On or Off Private Sub NumAndCaps(Optional OnOffNum% = 1 _ , Optional OnOffCaps% = 1) Dim o As OSVERSIONINFO o.dwOSVersionInfoSize = Len(o) GetVersionEx o Dim keys(0 To 255) As Byte: GetKeyboardState keys(0) If keys(&H90) <> OnOffNum Then ' Turn numlock on If o.dwPlatformId = 1 Then ' -> Win95/98 keys(&H90) = OnOffNum SetKeyboardState keys(0) ElseIf o.dwPlatformId = 2 Then ' -> WinNT keybd_event &H90, &H45, &H1 Or 0, 0 ' Simulate Key Press keybd_event &H90, &H45, &H1 Or &H2, 0 ' Simulate Key Release End If End If If keys(&H14) <> OnOffCaps Then ' Turn capslock If o.dwPlatformId = 1 Then ' -> Win95/98 keys(&H14) = OnOffCaps SetKeyboardState keys(0) ElseIf o.dwPlatformId = 2 Then ' -> WinNT keybd_event &H14, &H45, &H1 Or 0, 0 ' Simulate Key Press keybd_event &H14, &H45, &H1 Or &H2, 0 ' Simulate Key Release End If End If End Sub
Ainsi, pour Num lock seul: Sub OnOff() Call NumAndCaps(1, 0) End Sub
MP
"Emile" a écrit dans le message de news: OC#
Bonjour à tous,
Je cherche à enclencher "Num Lock" par VBA à l'ouverture d'une feuille Excel
quel que soit sont état précédent. J'en suis au test suivant: ==================================== > Private Declare Function GetKeyboardState Lib "user32" (pbKeyState As Byte)
As Long Sub NumState() Const VK_NUMLOCK = &H90 Dim KBState(0 To 255) As Byte GetKeyboardState KBState(0) MsgBox KBState(VK_NUMLOCK) '1 -> ON, 0 -> OFF End Sub ==================================== > Mais je ne sais pas comment lui retourner "1" si la réponse au test est "0"
pour endifier l'état.. :-(
Quelqu'un pourrait-il me mettre sur la voie ? :-)
Je vous en remercie d'avance, Cordialement.
Emile
Bonjour Emile;
Le fonctionnement de Num lock et Caps lock dépend de la version de ton
Operating System:
Option Explicit
Type OSVERSIONINFO
dwOSVersionInfoSize As Long
dwMajorVersion As Long
dwMinorVersion As Long
dwBuildNumber As Long
dwPlatformId As Long
szCSDVersion As String * 128
End Type
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
' Sets NUMLOCK and CAPSLOCK On or Off
Private Sub NumAndCaps(Optional OnOffNum% = 1 _
, Optional OnOffCaps% = 1)
Dim o As OSVERSIONINFO
o.dwOSVersionInfoSize = Len(o)
GetVersionEx o
Dim keys(0 To 255) As Byte: GetKeyboardState keys(0)
If keys(&H90) <> OnOffNum Then ' Turn numlock on
If o.dwPlatformId = 1 Then ' -> Win95/98
keys(&H90) = OnOffNum
SetKeyboardState keys(0)
ElseIf o.dwPlatformId = 2 Then ' -> WinNT
keybd_event &H90, &H45, &H1 Or 0, 0 ' Simulate Key Press
keybd_event &H90, &H45, &H1 Or &H2, 0 ' Simulate Key Release
End If
End If
If keys(&H14) <> OnOffCaps Then ' Turn capslock
If o.dwPlatformId = 1 Then ' -> Win95/98
keys(&H14) = OnOffCaps
SetKeyboardState keys(0)
ElseIf o.dwPlatformId = 2 Then ' -> WinNT
keybd_event &H14, &H45, &H1 Or 0, 0 ' Simulate Key Press
keybd_event &H14, &H45, &H1 Or &H2, 0 ' Simulate Key Release
End If
End If
End Sub
Ainsi, pour Num lock seul:
Sub OnOff()
Call NumAndCaps(1, 0)
End Sub
MP
"Emile" <emile63_noSpam@noSpam_isuisse.com> a écrit dans le message de news:
OC#yNRrWFHA.2540@tk2msftngp13.phx.gbl...
Bonjour à tous,
Je cherche à enclencher "Num Lock" par VBA à l'ouverture d'une feuille
Excel
quel que soit sont état précédent.
J'en suis au test suivant:
==================================== > Private Declare Function GetKeyboardState Lib "user32" (pbKeyState As
Byte)
As Long
Sub NumState()
Const VK_NUMLOCK = &H90
Dim KBState(0 To 255) As Byte
GetKeyboardState KBState(0)
MsgBox KBState(VK_NUMLOCK) '1 -> ON, 0 -> OFF
End Sub
==================================== >
Mais je ne sais pas comment lui retourner "1" si la réponse au test est
"0"
Bonjour Emile; Le fonctionnement de Num lock et Caps lock dépend de la version de ton Operating System: Option Explicit Type OSVERSIONINFO dwOSVersionInfoSize As Long dwMajorVersion As Long dwMinorVersion As Long dwBuildNumber As Long dwPlatformId As Long szCSDVersion As String * 128 End Type 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
' Sets NUMLOCK and CAPSLOCK On or Off Private Sub NumAndCaps(Optional OnOffNum% = 1 _ , Optional OnOffCaps% = 1) Dim o As OSVERSIONINFO o.dwOSVersionInfoSize = Len(o) GetVersionEx o Dim keys(0 To 255) As Byte: GetKeyboardState keys(0) If keys(&H90) <> OnOffNum Then ' Turn numlock on If o.dwPlatformId = 1 Then ' -> Win95/98 keys(&H90) = OnOffNum SetKeyboardState keys(0) ElseIf o.dwPlatformId = 2 Then ' -> WinNT keybd_event &H90, &H45, &H1 Or 0, 0 ' Simulate Key Press keybd_event &H90, &H45, &H1 Or &H2, 0 ' Simulate Key Release End If End If If keys(&H14) <> OnOffCaps Then ' Turn capslock If o.dwPlatformId = 1 Then ' -> Win95/98 keys(&H14) = OnOffCaps SetKeyboardState keys(0) ElseIf o.dwPlatformId = 2 Then ' -> WinNT keybd_event &H14, &H45, &H1 Or 0, 0 ' Simulate Key Press keybd_event &H14, &H45, &H1 Or &H2, 0 ' Simulate Key Release End If End If End Sub
Ainsi, pour Num lock seul: Sub OnOff() Call NumAndCaps(1, 0) End Sub
MP
"Emile" a écrit dans le message de news: OC#
Bonjour à tous,
Je cherche à enclencher "Num Lock" par VBA à l'ouverture d'une feuille Excel
quel que soit sont état précédent. J'en suis au test suivant: ==================================== > Private Declare Function GetKeyboardState Lib "user32" (pbKeyState As Byte)
As Long Sub NumState() Const VK_NUMLOCK = &H90 Dim KBState(0 To 255) As Byte GetKeyboardState KBState(0) MsgBox KBState(VK_NUMLOCK) '1 -> ON, 0 -> OFF End Sub ==================================== > Mais je ne sais pas comment lui retourner "1" si la réponse au test est "0"
pour endifier l'état.. :-(
Quelqu'un pourrait-il me mettre sur la voie ? :-)
Je vous en remercie d'avance, Cordialement.
Emile
Emile
Bonjour Michel, et merci. J'ai pas tout compris, mais j'ai testé et cela fonctionne :-) Toutefois y'aurait-il possibilité en connaissance de l' OS (Win XP Pro SP2) de faire plus simple ?
Je te remercie pour ton aide, Cordialement, Emile
"Michel Pierron" a écrit dans le message de news: % | Bonjour Emile; | Le fonctionnement de Num lock et Caps lock dépend de la version de ton | Operating System: | Option Explicit | Type OSVERSIONINFO | dwOSVersionInfoSize As Long | dwMajorVersion As Long | dwMinorVersion As Long | dwBuildNumber As Long | dwPlatformId As Long | szCSDVersion As String * 128 | End Type | 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 | | ' Sets NUMLOCK and CAPSLOCK On or Off | Private Sub NumAndCaps(Optional OnOffNum% = 1 _ | , Optional OnOffCaps% = 1) | Dim o As OSVERSIONINFO | o.dwOSVersionInfoSize = Len(o) | GetVersionEx o | Dim keys(0 To 255) As Byte: GetKeyboardState keys(0) | If keys(&H90) <> OnOffNum Then ' Turn numlock on | If o.dwPlatformId = 1 Then ' -> Win95/98 | keys(&H90) = OnOffNum | SetKeyboardState keys(0) | ElseIf o.dwPlatformId = 2 Then ' -> WinNT | keybd_event &H90, &H45, &H1 Or 0, 0 ' Simulate Key Press | keybd_event &H90, &H45, &H1 Or &H2, 0 ' Simulate Key Release | End If | End If | If keys(&H14) <> OnOffCaps Then ' Turn capslock | If o.dwPlatformId = 1 Then ' -> Win95/98 | keys(&H14) = OnOffCaps | SetKeyboardState keys(0) | ElseIf o.dwPlatformId = 2 Then ' -> WinNT | keybd_event &H14, &H45, &H1 Or 0, 0 ' Simulate Key Press | keybd_event &H14, &H45, &H1 Or &H2, 0 ' Simulate Key Release | End If | End If | End Sub | | Ainsi, pour Num lock seul: | Sub OnOff() | Call NumAndCaps(1, 0) | End Sub | | MP | | "Emile" a écrit dans le message de news: | OC# | > Bonjour à tous, | > | > Je cherche à enclencher "Num Lock" par VBA à l'ouverture d'une feuille | Excel | > quel que soit sont état précédent. | > J'en suis au test suivant: | > ==================================== | > Private Declare Function GetKeyboardState Lib "user32" (pbKeyState As | Byte) | > As Long | > Sub NumState() | > Const VK_NUMLOCK = &H90 | > Dim KBState(0 To 255) As Byte | > GetKeyboardState KBState(0) | > MsgBox KBState(VK_NUMLOCK) '1 -> ON, 0 -> OFF | > End Sub | > ==================================== | > | > Mais je ne sais pas comment lui retourner "1" si la réponse au test est | "0" | > pour endifier l'état.. :-( | > | > Quelqu'un pourrait-il me mettre sur la voie ? :-) | > | > Je vous en remercie d'avance, | > Cordialement. | > | > Emile | > | > | |
Bonjour Michel, et merci.
J'ai pas tout compris, mais j'ai testé et cela fonctionne :-)
Toutefois y'aurait-il possibilité en connaissance de l' OS (Win XP Pro SP2)
de faire plus simple ?
Je te remercie pour ton aide,
Cordialement,
Emile
"Michel Pierron" <michel.pierron@free.fr> a écrit dans le message de news:
%23lzCR7uWFHA.3760@TK2MSFTNGP15.phx.gbl...
| Bonjour Emile;
| Le fonctionnement de Num lock et Caps lock dépend de la version de ton
| Operating System:
| Option Explicit
| Type OSVERSIONINFO
| dwOSVersionInfoSize As Long
| dwMajorVersion As Long
| dwMinorVersion As Long
| dwBuildNumber As Long
| dwPlatformId As Long
| szCSDVersion As String * 128
| End Type
| 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
|
| ' Sets NUMLOCK and CAPSLOCK On or Off
| Private Sub NumAndCaps(Optional OnOffNum% = 1 _
| , Optional OnOffCaps% = 1)
| Dim o As OSVERSIONINFO
| o.dwOSVersionInfoSize = Len(o)
| GetVersionEx o
| Dim keys(0 To 255) As Byte: GetKeyboardState keys(0)
| If keys(&H90) <> OnOffNum Then ' Turn numlock on
| If o.dwPlatformId = 1 Then ' -> Win95/98
| keys(&H90) = OnOffNum
| SetKeyboardState keys(0)
| ElseIf o.dwPlatformId = 2 Then ' -> WinNT
| keybd_event &H90, &H45, &H1 Or 0, 0 ' Simulate Key Press
| keybd_event &H90, &H45, &H1 Or &H2, 0 ' Simulate Key Release
| End If
| End If
| If keys(&H14) <> OnOffCaps Then ' Turn capslock
| If o.dwPlatformId = 1 Then ' -> Win95/98
| keys(&H14) = OnOffCaps
| SetKeyboardState keys(0)
| ElseIf o.dwPlatformId = 2 Then ' -> WinNT
| keybd_event &H14, &H45, &H1 Or 0, 0 ' Simulate Key Press
| keybd_event &H14, &H45, &H1 Or &H2, 0 ' Simulate Key Release
| End If
| End If
| End Sub
|
| Ainsi, pour Num lock seul:
| Sub OnOff()
| Call NumAndCaps(1, 0)
| End Sub
|
| MP
|
| "Emile" <emile63_noSpam@noSpam_isuisse.com> a écrit dans le message de
news:
| OC#yNRrWFHA.2540@tk2msftngp13.phx.gbl...
| > Bonjour à tous,
| >
| > Je cherche à enclencher "Num Lock" par VBA à l'ouverture d'une feuille
| Excel
| > quel que soit sont état précédent.
| > J'en suis au test suivant:
| > ==================================== | > Private Declare Function GetKeyboardState Lib "user32" (pbKeyState As
| Byte)
| > As Long
| > Sub NumState()
| > Const VK_NUMLOCK = &H90
| > Dim KBState(0 To 255) As Byte
| > GetKeyboardState KBState(0)
| > MsgBox KBState(VK_NUMLOCK) '1 -> ON, 0 -> OFF
| > End Sub
| > ==================================== | >
| > Mais je ne sais pas comment lui retourner "1" si la réponse au test est
| "0"
| > pour endifier l'état.. :-(
| >
| > Quelqu'un pourrait-il me mettre sur la voie ? :-)
| >
| > Je vous en remercie d'avance,
| > Cordialement.
| >
| > Emile
| >
| >
|
|
Bonjour Michel, et merci. J'ai pas tout compris, mais j'ai testé et cela fonctionne :-) Toutefois y'aurait-il possibilité en connaissance de l' OS (Win XP Pro SP2) de faire plus simple ?
Je te remercie pour ton aide, Cordialement, Emile
"Michel Pierron" a écrit dans le message de news: % | Bonjour Emile; | Le fonctionnement de Num lock et Caps lock dépend de la version de ton | Operating System: | Option Explicit | Type OSVERSIONINFO | dwOSVersionInfoSize As Long | dwMajorVersion As Long | dwMinorVersion As Long | dwBuildNumber As Long | dwPlatformId As Long | szCSDVersion As String * 128 | End Type | 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 | | ' Sets NUMLOCK and CAPSLOCK On or Off | Private Sub NumAndCaps(Optional OnOffNum% = 1 _ | , Optional OnOffCaps% = 1) | Dim o As OSVERSIONINFO | o.dwOSVersionInfoSize = Len(o) | GetVersionEx o | Dim keys(0 To 255) As Byte: GetKeyboardState keys(0) | If keys(&H90) <> OnOffNum Then ' Turn numlock on | If o.dwPlatformId = 1 Then ' -> Win95/98 | keys(&H90) = OnOffNum | SetKeyboardState keys(0) | ElseIf o.dwPlatformId = 2 Then ' -> WinNT | keybd_event &H90, &H45, &H1 Or 0, 0 ' Simulate Key Press | keybd_event &H90, &H45, &H1 Or &H2, 0 ' Simulate Key Release | End If | End If | If keys(&H14) <> OnOffCaps Then ' Turn capslock | If o.dwPlatformId = 1 Then ' -> Win95/98 | keys(&H14) = OnOffCaps | SetKeyboardState keys(0) | ElseIf o.dwPlatformId = 2 Then ' -> WinNT | keybd_event &H14, &H45, &H1 Or 0, 0 ' Simulate Key Press | keybd_event &H14, &H45, &H1 Or &H2, 0 ' Simulate Key Release | End If | End If | End Sub | | Ainsi, pour Num lock seul: | Sub OnOff() | Call NumAndCaps(1, 0) | End Sub | | MP | | "Emile" a écrit dans le message de news: | OC# | > Bonjour à tous, | > | > Je cherche à enclencher "Num Lock" par VBA à l'ouverture d'une feuille | Excel | > quel que soit sont état précédent. | > J'en suis au test suivant: | > ==================================== | > Private Declare Function GetKeyboardState Lib "user32" (pbKeyState As | Byte) | > As Long | > Sub NumState() | > Const VK_NUMLOCK = &H90 | > Dim KBState(0 To 255) As Byte | > GetKeyboardState KBState(0) | > MsgBox KBState(VK_NUMLOCK) '1 -> ON, 0 -> OFF | > End Sub | > ==================================== | > | > Mais je ne sais pas comment lui retourner "1" si la réponse au test est | "0" | > pour endifier l'état.. :-( | > | > Quelqu'un pourrait-il me mettre sur la voie ? :-) | > | > Je vous en remercie d'avance, | > Cordialement. | > | > Emile | > | > | |