Jump to content
juanbeta15

Keyboard functions - Holding and releasing shift button

Recommended Posts

Well I've looked everywhere for this and couldn't find it. Tried the wiki and both the KeyUp/Down and VKeyUp/Down using VK_LSHIFT or VK_SHIFt or using 16 and the character input wouldn't work.

I don't know if the vkeys were changed or what, but when using VKeyDown and VK_LSHIFT I get a space instead of holding the shift button.

Link to comment
Share on other sites

{=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
procedure TypeSendEx(Text: string; PressEnter: Boolean);
Contributors: Wanted
Description: Types human like, presses enter if chosen.
Date Created: August 26th, 2011. By Wanted
Last Modified: December 31st, 2011. By Wanted
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=}

procedure TypeSendEx(Text: string; PressEnter: Boolean);
var
 UseShiftKey, ShiftKeyDown: Boolean;
 ShiftedKeys: string;
 I, L: Integer;
begin
 ShiftedKeys := 'ABCDEFGHIJKLMNOPQRSTUVWXZ~!@#$%^&*()_+{}|:"<>?';
 L := Length(Text);
 ShiftKeyDown := False;
 for I := 1 to L do
 begin
   UseShiftKey := StrInStr(Text[i], ShiftedKeys);
   if (not (ShiftKeyDown)) then
   begin
     ShiftKeyDown := True;
     KeyDown(VK_SHIFT);
   end;
   if ((not (UseShiftKey)) and (ShiftKeyDown)) then
   begin
     KeyUp(VK_SHIFT);
     ShiftKeyDown := False;
   end;
   PushKey(Text[i]);
   WaitRR(40, 80);
 end;
 if (PressEnter) then
 begin
   PushKey(Chr(13));
   WaitRR(40, 80);
 end;
end;

 

Source code from OSI1 a long time ago

 

should answer your questions

Link to comment
Share on other sites

I see the code uses the KeyDown function with the VK_LSHIFT virtual key as input, but this is where I have my problem.

Here's the basic idea of what I'm trying to do.

 

KeyDown(VK_SHIFT)
PressKey('a');
KeyUp(VK_SHIFT);

 

But when I run this using either KeyDown/Up or VKeyDown/Up or PressKey or PressVKey I yield the same results.

I need to be able to leave shift pressed while being able to do other functions.

Edited by juanbeta15
Link to comment
Share on other sites

I see the code uses the KeyDown function with the VK_LSHIFT virtual key as input, but this is where I have my problem.

Here's the basic idea of what I'm trying to do.

 

KeyDown(VK_SHIFT)
PressKey('a');
KeyUp(VK_SHIFT);

 

But when I run this using either KeyDown/Up or VKeyDown/Up or PressKey or PressVKey I yield the same results.

I need to be able to leave shift pressed while being able to do other functions.

 

I am not really sure why it's not acting as one would expect, but I made a small work-around which might serve your needs.

program New;

procedure KeyDownEx(Key: Char);
var  
 vkcode:Byte;
begin
 vkCode := ord(lowercase(key)[1]);
 if (GetCurrentKeyState(VK_SHIFT) and GetToggleKeyState(VK_CAPITAL)) then 
   vkCode := ord(lowercase(key)[1])
 else if GetCurrentKeyState(VK_SHIFT) then   
   vkCode := ord(uppercase(key)[1]);       

 if GetCurrentKeyState(VK_SHIFT) then      
 begin                           
   if (Key in ['1','2','3','4','5','6','7','8','9']) then
     vkCode := vkCode - 16;
   if (Key in ['0']) then
     vkCode := vkCode + 13;
 end;                                  
 KeyDown(chr(vkCode));
end;


procedure KeyUpEx(Key: Char);
var vkcode:Byte;
begin
 vkCode := ord(lowercase(key)[1]);                                          
 if (GetCurrentKeyState(VK_SHIFT) and GetToggleKeyState(VK_CAPITAL)) then 
   vkCode := ord(lowercase(key)[1])
 else if GetCurrentKeyState(VK_SHIFT) then   
   vkCode := ord(uppercase(key)[1]);       

 if GetCurrentKeyState(VK_SHIFT) then      
 begin                           
   if (Key in ['1','2','3','4','5','6','7','8','9']) then
     vkCode := vkCode - 16;
   if (Key in ['0']) then
     vkCode := vkCode + 13;
 end;               
 WriteLn(vkCode);               
 KeyUp(chr(vkCode));                                                                      
end;                                                       


begin                                      
 Wait(500);  

 VKeyDown(VK_SHIFT);                                                            
 KeyDownEx('a');                                 
 KeyUpEx('a');                                                        
 VKeyUp(VK_SHIFT);                       
end.  

 

This is a very stinky and "hacky" solution.. But it might work "good enough".

Edited by slacky
Link to comment
Share on other sites

Thanks Slacky, it really helped.

One more thing, seeing what you sent, it is currently not possible to shift-click?

I'm trying to make an auto seller in a game , which requires shift-clicking an item to sell it.

Is there any way to work around this?

What do you mean by "shift-click"?

Link to comment
Share on other sites

The action of clicking while having shift held down.

In this game, to sell an item, you have hold the shift button down and click the item.

Like if you shift click some text, it will highlight it.

 

Or if you control click a link, it will open it in a new tab. If you shift click a link, it will open it in a new window.

Edited by juanbeta15
Link to comment
Share on other sites

What I saw is that when using PressKey(chr(NUMBER)), the key pressed is roughly the equivalent of pressing ALT + NUMBER

But this doesn't apply to every number, that's the weird part.

 

program Comparing_PressKey;
var
 i: Integer;
begin
for i := 1 to 300 do
  begin
  PressKey(chr(i));
  WriteLn(IntToStr(i)+' '+chr(i));
  end;
end.

 

For example, if you run this and compare specifically these results taken from Scar's Debug dialoge:

290 "

291 #

292 $

293 %

294 &

295 '

296 (

297 )

298 *

299 +

300 ,

 

 

And if you type in ALT + 300 you get ",".

This occurs for a lot of the numbers. But for example, the first printed characters are:

b7K0zZq.png

 

But when pressing ALT + 1 or 2 I get different characters.

 

Lol, I don't know what does this have to do with the problem, but its what I noticed.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
  • Create New...