views:

366

answers:

2

I have a String that I needed access to the first character of, so I used stringname[1]. With the unicode support this no longer works. I get an error: [DCC Error] sndkey32.pas(420): E2010 Incompatible types: 'Char' and 'AnsiChar'

Example code:

//vkKeyScan from the windows unit

var
KeyString : String[20];
MKey : Word;

mkey:=vkKeyScan(KeyString[1])

How would I write this in modern versions of Delphi

A: 

Off the top of my head: do you really need a string, which is equal to widestring in Delphi 2009?

One option is to have the definition var KeyString: AnsiString;

then when you take KeyString[1] that would be an AnsiChar rather than a Char.

Muhammad Alkarouri
I made a mistake on the initial question, and have fixed it. I had tried AnsiString, but it doesn't seem to work as KeyString : AnsiString[20]; thanks for the help.
Brad
The problem is the opposite. the vkKeyScan function *wants* a WideChar! And String[20] is not a Unicode string, but a ShortString, due to the "[20]" part.
Andreas Rejbrand
+5  A: 

The type String[20] is a ShortString of length 20, i.e. a ShortString that contains 20 characters. But ShortStrings behave like AnsiStrings, i.e. they are not Unicode - one character is one byte. Thus KeyString[1] is an AnsiChar, whereas the vkKeyScan function expects a WideChar (=Char) as argument. I really have no idea whatsoever why you want to use the type String[20] instead of String (=UnicodeString), but you could convert the AnsiChar KeyString[1] to a WideChar:

mkey := vkKeyScan(WideChar(KeyString[1]))
Andreas Rejbrand