detecting the SHIFT key on the Mac

  • Thread starter Laurent (de la cabane au Canada)
  • Start date
J

JE McGimpsey

Bob Greenblatt said:
I don¹t know what the VB statement ³system² is. It¹s not in the object
browser. Perhaps it¹s an apple script object, not VB. In any case, what
Bernard was trying to explain is in VB, it¹s Application.operatingsystem.
This returns a string like²Macintosh (PowerPC) 10.38². So to determine the
platform look at the left 3 characters.

I know I'm jumping in late, but I wouldn't mess with Application.System
at all. Rather I'd use conditional compilation:


#If Mac Then

Public Sub DetectShiftKey()
Dim sTemp As String
Dim sScript As String
sScript = ("if (keys pressed) contains {""Shift""} then" & _
vbCr & "set result to ""Shift Key depressed""" & _
vbCr & "else" & vbCr & _
"set result to ""Shift Key not depressed""" & vbCr & _
"end if")
sTemp = MacScript(sScript)
MsgBox "On the Mac: " & vbCr & sTemp
End Sub

#Else

Private Declare Function GetKeyState Lib "User32" _
(ByVal vKey As Long) As Integer

Public Sub DetectShiftKey()
If GetKeyState(&H10) < 0 Then MsgBox "Touche MAJ Active."
End Sub

#End If

that way, only the relevant code is even compiled, much less executed.

On my cross-platform apps, I segregate the platform specific code into a
module like this - for instance, my Replace() method is stuck into a
conditionally compiled module

#If Mac Then
Public Sub Replace(...)
...
End Sub
#End If

so that WinXL2000/2002/2003 uses the native VBA6 method, and MacXL uses
mine, but I don't have to use workarounds in the main code.
 
B

Bernard Rey

JE McGimpsey:
I know I'm jumping in late, but I wouldn't mess with Application.System
at all. Rather I'd use conditional compilation:

#If Mac Then .../... #Else .../... #End If

that way, only the relevant code is even compiled, much less executed.

On my cross-platform apps, I segregate the platform specific code into a
module like this - for instance, my Replace() method is stuck into a
conditionally compiled module
.../...
so that WinXL2000/2002/2003 uses the native VBA6 method, and MacXL uses
mine, but I don't have to use workarounds in the main code.

This is of course right. But Laurent insisted that he didn't know much about
Macs. And it may be easier for him to keep with some minor ajustements rather
than writing a really optimized code for the Mac (if there is, as he didn't
give much information about what his code was doing, except that it needed to
know if the Shift key was depressed when clicking on a button).

Well, anyway, now he'll have the complete set of tools ;-)
 
L

Laurent (de la cabane au Canada)

JE said:
I know I'm jumping in late, but I wouldn't mess with Application.System
at all. Rather I'd use conditional compilation:


#If Mac Then

Wow! I didn't know conditionnal compilation even existed in VBA!
Yesterday I was about to abandon writing a mac version of my macro, and
now I have a solution that is getting better and better with every
contribution.

You guys are terrific! Thanks to all of you,

Laurent
 
J

JE McGimpsey

Bernard Rey said:
This is of course right. But Laurent insisted that he didn't know much about
Macs. And it may be easier for him to keep with some minor ajustements rather
than writing a really optimized code for the Mac (if there is, as he didn't
give much information about what his code was doing, except that it needed to
know if the Shift key was depressed when clicking on a button).

Don't disagree with your premise at all, and you're probably right.

For me, conditional compilation is much easier to work with than
modifying code to evaluate and adjust for the platform at run-time.

Probably just the way I think...
Well, anyway, now he'll have the complete set of tools ;-)

Oh, there may be a few tricks left...<g>
 
B

Bob Greenblatt

Don't disagree with your premise at all, and you're probably right.

For me, conditional compilation is much easier to work with than
modifying code to evaluate and adjust for the platform at run-time.

Probably just the way I think...


Oh, there may be a few tricks left...<g>

I guess it's just a matter of style. I have never used conditional
compilation, but instead detect the platform at run time. If it gets messy
it's still pretty easy to isolate all the platform specific code.
 
P

Paul Berkowitz

I know I'm jumping in late, but I wouldn't mess with Application.System
at all. Rather I'd use conditional compilation:


#If Mac Then

When I read up on this earlier in the Help, it indicated that in order for
that line to make sense, you'd first have to hard code

Mac = 1 ' on Macs

or

Mac = 0 ' on Windows


in the module. I.e. 'Mac' isn't a built-in application property or anything
like that - you have to set it. (Presumably you'd Dim it as a Const?) Am I
understanding that correctly? I don't see it in your code, JE, which makes
me wonder. I didn't do this because I figured Laurent might not want to have
to hard-code each version separately for different users.

--
Paul Berkowitz
MVP MacOffice
Entourage FAQ Page: <http://www.entourage.mvps.org/faq/index.html>
AppleScripts for Entourage: <http://macscripter.net/scriptbuilders/>

Please "Reply To Newsgroup" to reply to this message. Emails will be
ignored.

PLEASE always state which version of Microsoft Office you are using -
**2004**, X or 2001. It's often impossible to answer your questions
otherwise.
 
P

Paul Berkowitz

Bob Greenblatt:


Place it in the "/Library/ScriptingAdditions/" Folder.

Or ~/Library/ScriptingAdditions/ folder (make one if necessary). Then it
works just in your own user. That's where I put my scripting additions so
that I can have test users with no extra additions.

--
Paul Berkowitz
MVP MacOffice
Entourage FAQ Page: <http://www.entourage.mvps.org/faq/index.html>
AppleScripts for Entourage: <http://macscripter.net/scriptbuilders/>

Please "Reply To Newsgroup" to reply to this message. Emails will be
ignored.

PLEASE always state which version of Microsoft Office you are using -
**2004**, X or 2001. It's often impossible to answer your questions
otherwise.
 
P

Paul Berkowitz

I know what's happening here: you've been testing with MS Word, where
OperationSystem *is* a Property of System. But this is not so in Excel (where
there is no System class at all. In an Excel macro, System.OperatingSystem
results in an immediate syntax error.

Correct. Strangely, I clicked on the "Microsoft Excel VBA Help" first - but
I was in Word at the time. Under Application, I could not find any
OperatingSystem property. Evidently I was plugged into Word's Application
entry, not Excel's, in spite of my trying to go to Excel VBA Help. Hmmm. And
there _was_ a System property and object.

I did not check the Object Browser as well - that was my mistake. I won't
repeat it!


--
Paul Berkowitz
MVP MacOffice
Entourage FAQ Page: <http://www.entourage.mvps.org/faq/index.html>
AppleScripts for Entourage: <http://macscripter.net/scriptbuilders/>

Please "Reply To Newsgroup" to reply to this message. Emails will be
ignored.

PLEASE always state which version of Microsoft Office you are using -
**2004**, X or 2001. It's often impossible to answer your questions
otherwise.
 
J

JE McGimpsey

Paul Berkowitz said:
When I read up on this earlier in the Help, it indicated that in order for
that line to make sense, you'd first have to hard code

Mac = 1 ' on Macs

or

Mac = 0 ' on Windows


in the module. I.e. 'Mac' isn't a built-in application property or anything
like that - you have to set it. (Presumably you'd Dim it as a Const?) Am I
understanding that correctly? I don't see it in your code, JE, which makes
me wonder. I didn't do this because I figured Laurent might not want to have
to hard-code each version separately for different users.

You can define your own compiler constants (which you then have to set,
either with the #Const directive, or using the Conditional Compilation
Arguments textbox in the General tab of the VBE's Tools/<project>
Properties dialog) but three are predefined (see "Compiler Constants" in
XL/VBA Help): Win16, Win32, Mac.
 
P

Paul Berkowitz

You can define your own compiler constants (which you then have to set,
either with the #Const directive, or using the Conditional Compilation
Arguments textbox in the General tab of the VBE's Tools/<project>
Properties dialog) but three are predefined (see "Compiler Constants" in
XL/VBA Help): Win16, Win32, Mac.


Thanks, JE. Well in that case 'If Mac' certainly seems the easiest way to
go, I'd agree. It's odd that the VB Help entry " Strategies for Developing
Cross-Platform Solutions" says:

Use Conditional Compilation
<snip>

For example, if you want to write a file search function that runs on either
platform, you can create a conditional compiler constant and set it to
indicate whether code is compiled for Windows or the Macintosh. To indicate
that code is compiled for the Macintosh, click Project Properties on the
Tools menu in the Visual Basic Editor, and then type Mac = 1 in the
Conditional Compilation Arguments box. The following code would then compile
and run on the Macintosh. In Windows, you would set the same constant to 0
(zero) to compile and run the code in Windows.

#If Mac Then
...


It's odd they tell you to do all that if it's not necessary...


--
Paul Berkowitz
MVP MacOffice
Entourage FAQ Page: <http://www.entourage.mvps.org/faq/index.html>
AppleScripts for Entourage: <http://macscripter.net/scriptbuilders/>

Please "Reply To Newsgroup" to reply to this message. Emails will be
ignored.

PLEASE always state which version of Microsoft Office you are using -
**2004**, X or 2001. It's often impossible to answer your questions
otherwise.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top