Click here to Skip to main content
15,896,493 members
Please Sign up or sign in to vote.
1.50/5 (2 votes)
See more:
I am trying to compile my originally visual c++ project on Xcode so I can run it on macOSX and as I can't use windows.h in Xcode/mac I'm looking for alternatives.
Could you give me some ways to replace these subroutines:
UINT SendString(string str)
{
   UINT count = 0;
   for (size_t i = 0; i < str.size();i++)
   {   
      INPUT input[2];
      ::ZeroMemory(input, sizeof(input)); 
      input[0].type = input[1].type = INPUT_KEYBOARD;
      input[0].ki.wVk  = input[1].ki.wVk = VkKeyScan(str[i]);
      input[1].ki.dwFlags = KEYEVENTF_KEYUP;
      count += SendInput(2, input, sizeof(INPUT));
   }
   return count;
}
void SendKey(char key){
	keybd_event(VkKeyScan(key),0,0,0);
	keybd_event(VkKeyScan(key),0,2,0);
}
void MouseMove(int x, int y )
{  
	double fScreenWidth    = ::GetSystemMetrics( SM_CXSCREEN )-1; 
	double fScreenHeight  = ::GetSystemMetrics( SM_CYSCREEN )-1; 
	double fx = x*(65535.0f/fScreenWidth);
	double fy = y*(65535.0f/fScreenHeight);
	INPUT  Input={0};
	Input.type      = INPUT_MOUSE;
	Input.mi.dwFlags  = MOUSEEVENTF_MOVE|MOUSEEVENTF_ABSOLUTE;
	Input.mi.dx = fx;
	Input.mi.dy = fy;
	::SendInput(1,&Input,sizeof(INPUT));
}
void MouseClick(string button, string mode){
	INPUT    Input={0};
	Input.type = INPUT_MOUSE;
	if(mode == "down"){
		if(button == "left"){Input.mi.dwFlags=MOUSEEVENTF_LEFTDOWN;}else if(button == "right"){Input.mi.dwFlags=MOUSEEVENTF_RIGHTDOWN;}
		::SendInput(1,&Input,sizeof(INPUT));
	}else if(mode == "up"){
		::ZeroMemory(&Input,sizeof(INPUT));
		if(button=="left"){Input.mi.dwFlags=MOUSEEVENTF_LEFTUP;}else if(button=="right"){Input.mi.dwFlags=MOUSEEVENTF_RIGHTUP;}
		::SendInput(1,&Input,sizeof(INPUT));
	}else if(mode == "both"){
		if(button == "left"){Input.mi.dwFlags=MOUSEEVENTF_LEFTDOWN;}else if(button == "right"){Input.mi.dwFlags=MOUSEEVENTF_RIGHTDOWN;}
		::SendInput(1,&Input,sizeof(INPUT));
		::ZeroMemory(&Input,sizeof(INPUT));
		if(button=="left"){Input.mi.dwFlags=MOUSEEVENTF_LEFTUP;}else if(button=="right"){Input.mi.dwFlags=MOUSEEVENTF_RIGHTUP;}
		::SendInput(1,&Input,sizeof(INPUT));
}
}

ShellExecute(NULL, L"open", L"http://google.com", NULL, NULL, SW_SHOWNORMAL);
Posted
Updated 13-Dec-12 1:02am
v2
Comments
Sergey Alexandrovich Kryukov 12-Dec-12 22:53pm    
What's the platform? Tag it. What's the problem? Having declarations from Windows API? You can replace some of them, such as UINT, not those which are Windows-specific...
--SA
Jochen Arndt 13-Dec-12 3:08am    
You should rephrase your question. I assume you mean something like 'How to convert this Windows code to OS X / iOS using Xcode'.
But there is no simple conversion. You probably must write new code that does the the same tasks using the OS X API. I never wrote programs for OS X / iOS but I'm quite sure that there are no direct replacement functions for SendInput() and ShellExecute().
Pablo Aliskevicius 13-Dec-12 4:50am    
Maybe not iOS, but Linux?
Jochen Arndt 13-Dec-12 5:02am    
Using Xcode to develop Linux applications seems to be an advanced task. I assume OS X, eventually iOS. But only the questioner knows.
Jochen Arndt 13-Dec-12 8:39am    
With the updated question it is now clear what you want. But I already gave you the answer:
You must find OS X API functions that perform the required tasks. For keyboard events (simulating key press events), this might be CGEventCreateKeyboardEvent() and CGEventPost().

The OS X and Windows API are totally different. So you must write completely new code.

1 solution

Text from comments to have an answer.

The OS X and Windows APIs are totally different. So you must write completely new code.

You can try to find OS X API functions that perform the same tasks as the Windows API functions. For keyboard events (simulating key press events), this might be CGEventCreateKeyboardEvent() and CGEventPost().
 
Share this answer
 
Comments
Albert Holguin 13-Dec-12 13:05pm    
+5, supporting multiple platforms after the fact is always a pain (i.e. not programming for it in advance)
Jochen Arndt 13-Dec-12 13:54pm    
Thank you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900