Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to turn a string into a Const

Status
Not open for further replies.

john230873

Programmer
Oct 3, 2003
89
NZ
I have this issue that I think I need to use pointer for but unsure how. I have a database that uses can enter in key stokes that they want to trigger. For this to work I need to be able to pass a string to the keydb_event.

Here is an example of what I am thing to do

procedure TForm1.Button1Click(Sender: TObject);
var
MySTRKey : String;
begin
MySTRKey := 'VK_MENU';
keybd_event( MySTRKey,0,0,0);
end;

Of cause the MySTRKey is dynamic in the real program. When I run this I get incompatibly type Bytes and String.

This would work.


procedure TForm1.Button1Click(Sender: TObject);
var
MySTRKey : String;
begin
keybd_event( VK_MENU,0,0,0);
end;

How can I pass this pram dynamically
 
Rather than defining it as a string trying using an integer since the value of VK_MENU is numeric.

function TForm1.SetValue(ValueToSet: Integer): Boolean;
begin
//defined in the global parameters
parameter := Ord(ValueToSet);

result := True;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
keybd_event(parameter,0,0,0);
end;

Shuesty
 
Remember that the users defines their own key stokes. It would be far easiler for them to remember VT_TAB then 12 (or is it 8).

 
Why not use what Shuesty has provided. You could populate a combobox with the names of all the virtual codes (VK_TAB, VK_MENU etc.) and have an array of integers which correspond to the list of virtual codes.

When the user selects an item in the combobox, you read the ItemIndex property of the combobox and use that as the position of the equivalent value in the array of integers. You can pass this as an argument into Shuesty's SetValue method
e.g.
Code:
SetValue(ArrayOfIntegers[ComboBox1.ItemIndex]);

Also check out the Delphi help file for TComboBox - I have a feeling you may not need to have a separate array for the integers. I think a combobox has an additional property for each item, into which you can store the corresponding integer (I do not have Delphi in front of me!).

Clive [infinity]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"To err is human, but to really foul things up you need a computer."
Paul Ehrlich
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To get the best answers from this forum see: faq102-5096
 
Thanks Guys I thought of that ie another table in the database which I might end up doing but now I just wondering how to say read in a string but allow it to be repersented in a line of code as an ord.

 
Sorry I should say the reason for this is so I can use its value in CASE statements
 
The only way I can see that working is predefining the relationship between the string and ordinals somewhere in the code. Whether it's from your database, a config file you load, an internal array, or the combo box idea ... you need to define the relationship before using it in a Case block. Or you could use Delphi's String equivalent to the Ord's Case statments ... a whack load of if statements.

Shuesty
 
Thanks to everyone who replied. In the end I am using a database to store the user key requests so I am going to add another table to store the Key and its byte value. If written correctly with one query I can get all the key values and Ords which I think will be quicker than using a case statement with about 250 conditions
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top